Class: Kontena::Cli::Vpn::CreateCommand
- Inherits:
-
Kontena::Command
- Object
- Clamp::Command
- Kontena::Command
- Kontena::Cli::Vpn::CreateCommand
- Includes:
- Common, GridOptions, Stacks::StacksHelper
- Defined in:
- lib/kontena/cli/vpn/create_command.rb
Instance Attribute Summary
Attributes inherited from Kontena::Command
#arguments, #exit_code, #result
Instance Method Summary collapse
- #execute ⇒ Object
- #find_node(token, preferred_node = nil) ⇒ Object
- #node_vpn_ip(node) ⇒ String
- #wait_for_configuration_to_finish(token) ⇒ Object
Methods included from Stacks::StacksHelper
#wait_for_deploy_to_finish, #wait_for_deployment_to_start, #wait_for_service_deploy
Methods included from GridOptions
Methods included from Common
#access_token=, #add_master, #any_key_to_continue, #any_key_to_continue_with_timeout, #api_url, #api_url=, #caret, #clear_current_grid, #client, #cloud_auth?, #cloud_client, #config, #confirm, #confirm_command, #current_grid, #current_master_index, #debug?, #display_account_login_info, #display_login_info, display_logo, #display_master_login_info, #error, exit_with_error, #kontena_account, #logger, #pastel, #print, #prompt, #puts, #require_api_url, #require_token, #reset_client, #reset_cloud_client, #running_quiet?, #running_silent?, #running_verbose?, #spin_if, #spinner, #sprint, #sputs, #stdin_input, #use_refresh_token, #vfakespinner, #vputs, #vspinner, #warning
Methods inherited from Kontena::Command
banner, callback_matcher, #help_requested?, inherited, #instance, load_subcommand, requires_current_account_token, requires_current_account_token?, requires_current_grid, requires_current_grid?, requires_current_master, requires_current_master?, requires_current_master_token, requires_current_master_token?, #run, #run_callbacks, #verify_current_account_token, #verify_current_grid, #verify_current_master, #verify_current_master_token
Instance Method Details
#execute ⇒ Object
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 57 58 |
# File 'lib/kontena/cli/vpn/create_command.rb', line 12 def execute require_api_url token = require_token preferred_node = node name = 'vpn' vpn = client(token).get("stacks/#{current_grid}/#{name}") rescue nil exit_with_error('Vpn stack already exists') if vpn node = find_node(token, preferred_node) vpn_ip = node_vpn_ip(node) data = { name: name, stack: 'krates/vpn', version: Kontena::Cli::VERSION, registry: 'file://', source: '---', expose: 'server', services: [ name: 'server', stateful: true, image: 'krates/openvpn:latest', 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}/stacks", data) deployment = client(token).post("stacks/#{current_grid}/#{name}/deploy", {}) spinner "Deploying #{pastel.cyan(name)} service " do wait_for_deploy_to_finish(deployment) end spinner "Generating #{pastel.cyan(name)} keys (this will take a while) " do wait_for_configuration_to_finish(token) end puts "#{pastel.cyan(name)} service is now started (udp://#{vpn_ip}:1194)." puts "use 'kontena vpn config' to fetch OpenVPN client config to your machine." end |
#find_node(token, preferred_node = nil) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/kontena/cli/vpn/create_command.rb', line 73 def find_node(token, preferred_node = nil) nodes = client(token).get("grids/#{current_grid}/nodes") if preferred_node.nil? node = nodes['nodes'].find{|n| n['connected'] && !n['public_ip'].to_s.empty?} exit_with_error('Cannot find any online nodes with public ip. If you want to connect with private address, please use --node and/or --ip options.') if node.nil? else node = nodes['nodes'].find{|n| n['connected'] && n['name'] == preferred_node } exit_with_error('Node not found') if node.nil? end node end |
#node_vpn_ip(node) ⇒ String
88 89 90 91 92 93 94 95 96 97 |
# File 'lib/kontena/cli/vpn/create_command.rb', line 88 def node_vpn_ip(node) return ip unless ip.nil? # vagrant if node['labels'] && node['labels'].include?('provider=vagrant') node['private_ip'].to_s else node['public_ip'].to_s.empty? ? node['private_ip'].to_s : node['public_ip'].to_s end end |
#wait_for_configuration_to_finish(token) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/kontena/cli/vpn/create_command.rb', line 60 def wait_for_configuration_to_finish(token) finished = false payload = {cmd: ['/usr/local/bin/ovpn_getclient', 'KONTENA_VPN_CLIENT']} service = client(require_token).get("services/#{current_grid}/vpn/server/containers", payload)['containers'][0] until finished sleep 3 stdout, stderr = client(require_token).post("containers/#{service['id']}/exec", payload) finished = true if stdout.join('').include?('BEGIN PRIVATE KEY'.freeze) end finished end |