Sha256: 5155e31e6d889ddc71406a80d0fba15247a736aca53ec8477c2936b31970d227

Contents?: true

Size: 1.93 KB

Versions: 10

Compression:

Stored size: 1.93 KB

Contents

require 'shell-spinner'

module Kontena::Cli::Vpn
  class CreateCommand < Clamp::Command
    include Kontena::Cli::Common

    option '--node', 'NODE', 'Node name where VPN is deployed'
    option '--ip', 'IP', 'Node ip-address'

    def execute
      require_api_url
      token = require_token
      preferred_node = node

      vpn = client(token).get("services/#{current_grid}/vpn") rescue nil
      abort('Vpn already exists') if vpn

      nodes = client(token).get("grids/#{current_grid}/nodes")
      if preferred_node.nil?
        node = nodes['nodes'].find{|n| n['connected']}
        abort('Cannot find any online nodes') if node.nil?
      else
        node = nodes['nodes'].find{|n| n['connected'] && n['name'] == preferred_node }
        abort('Node not found') if node.nil?
      end

      vpn_ip = node_vpn_ip(node)
      data = {
        name: 'vpn',
        stateful: true,
        image: 'kontena/openvpn:ethwe',
        ports: [
          {
            container_port: '1194',
            node_port: '1194',
            protocol: 'udp'
          }
        ],
        cap_add: ['NET_ADMIN'],
        env: ["OVPN_SERVER_URL=udp://#{vpn_ip}:1194"],
        affinity: ["node==#{node['name']}"]
      }
      client(token).post("grids/#{current_grid}/services", data)
      client(token).post("services/#{current_grid}/vpn/deploy", {})
      ShellSpinner "Deploying vpn service " do
        sleep 1 until client(token).get("services/#{current_grid}/vpn")['state'] != 'deploying'
      end
      puts "OpenVPN service is now started (udp://#{vpn_ip}:1194)."
      puts "Use 'kontena vpn config' to fetch OpenVPN client config to your machine (it takes a while until config is ready)."
    end

    # @param [Hash] node
    # @return [String]
    def node_vpn_ip(node)
      return ip unless ip.nil?

      # vagrant
      if api_url == 'http://192.168.66.100:8080'
        node['private_ip']
      else
        node['public_ip']
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
kontena-cli-0.11.7 lib/kontena/cli/vpn/create_command.rb
kontena-cli-0.11.6 lib/kontena/cli/vpn/create_command.rb
kontena-cli-0.11.5 lib/kontena/cli/vpn/create_command.rb
kontena-cli-0.11.4 lib/kontena/cli/vpn/create_command.rb
kontena-cli-0.11.3 lib/kontena/cli/vpn/create_command.rb
kontena-cli-0.11.2 lib/kontena/cli/vpn/create_command.rb
kontena-cli-0.11.1 lib/kontena/cli/vpn/create_command.rb
kontena-cli-0.11.0 lib/kontena/cli/vpn/create_command.rb
kontena-cli-0.10.3 lib/kontena/cli/vpn/create_command.rb
kontena-cli-0.10.2 lib/kontena/cli/vpn/create_command.rb