Sha256: 7961b7c7c1efc3caecd389d17e09807c04c3204c3e3643a9fb2a334b8df76b36

Contents?: true

Size: 1.7 KB

Versions: 8

Compression:

Stored size: 1.7 KB

Contents

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

      public_ip = ip || node['public_ip']

      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://#{public_ip}:1194"],
        affinity: ["node==#{node['name']}"]
      }
      client(token).post("grids/#{current_grid}/services", data)
      result = client(token).post("services/#{current_grid}/vpn/deploy", {})
      print 'deploying '
      until client(token).get("services/#{current_grid}/vpn")['state'] != 'deploying' do
        print '.'
        sleep 1
      end
      puts ' done'
      puts "OpenVPN service is now started (udp://#{public_ip}:1194)."
      puts "Use 'kontena vpn config' to fetch OpenVPN client config to your machine (it takes a while until config is ready)."
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
kontena-cli-0.9.1 lib/kontena/cli/vpn/create_command.rb
kontena-cli-0.9.0 lib/kontena/cli/vpn/create_command.rb
kontena-cli-0.8.4 lib/kontena/cli/vpn/create_command.rb
kontena-cli-0.8.3 lib/kontena/cli/vpn/create_command.rb
kontena-cli-0.8.2 lib/kontena/cli/vpn/create_command.rb
kontena-cli-0.8.1 lib/kontena/cli/vpn/create_command.rb
kontena-cli-0.8.0.1 lib/kontena/cli/vpn/create_command.rb
kontena-cli-0.8.0 lib/kontena/cli/vpn/create_command.rb