Class: Central::Cli::Vpn::CreateCommand

Inherits:
Clamp::Command
  • Object
show all
Includes:
Common, StackOptions
Defined in:
lib/central/cli/vpn/create_command.rb

Instance Method Summary collapse

Methods included from StackOptions

included

Methods included from Common

#access_token=, #add_master, #api_url, #api_url=, #clear_current_stack, #client, #current_master, #current_master=, #current_master_index, #current_stack, #current_stack=, #ensure_custom_ssl_ca, #require_api_url, #require_current_stack, #require_token, #reset_client, #save_settings, #settings, #settings_filename

Instance Method Details

#executeObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/central/cli/vpn/create_command.rb', line 11

def execute
  require_api_url
  token = require_token
  preferred_node = node

  vpn = begin
          client(token).get("services/#{current_stack}/vpn")
        rescue
          nil
        end
  abort('VPN already exists') if vpn

  nodes = client(token).get("stacks/#{current_stack}/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: 'fishyard/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("stacks/#{current_stack}/services", data)
  client(token).post("services/#{current_stack}/vpn/deploy", {})
  ShellSpinner 'Deploying vpn service ' do
    sleep 1 until client(token).get("services/#{current_stack}/vpn")['state'] != 'deploying'
  end
  puts "OpenVPN service is now started (udp://#{vpn_ip}:1194)."
  puts "Use 'cm vpn config' to fetch OpenVPN client config to your machine "\
       '(it takes a while until config is ready).'
end

#node_vpn_ip(node) ⇒ String

Parameters:

  • node (Hash)

Returns:

  • (String)


60
61
62
63
64
65
66
67
68
69
# File 'lib/central/cli/vpn/create_command.rb', line 60

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