kontena-cli 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,20 @@
1
+ require 'kontena/client'
2
+ require_relative '../common'
3
+
4
+ module Kontena::Cli::Grids
5
+ class AuditLog
6
+ include Kontena::Cli::Common
7
+
8
+ def show(options)
9
+ require_api_url
10
+ require_current_grid
11
+ token = require_token
12
+ audit_logs = client(token).get("grids/#{current_grid}/audit_log", {limit: options.limit})
13
+ puts '%-30.30s %-10s %-15s %-25s %-15s %-25s %-15s %-15s' % ['Time', 'Grid', 'Resource Type', 'Resource Name', 'Event Name', 'User', 'Source IP', 'User-Agent']
14
+ audit_logs['logs'].each do |log|
15
+ puts '%-30.30s %-10s %-15s %-25s %-15s %-25s %-15s %-15s' % [ log['time'], log['grid'], log['resource_type'], log['resource_name'], log['event_name'], log['user_identity']['email'], log['source_ip'], log['user_agent']]
16
+ end
17
+
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,89 @@
1
+ module Kontena::Cli::Grids; end;
2
+
3
+ require_relative 'grids'
4
+ require_relative 'users'
5
+ require_relative 'audit_log'
6
+
7
+
8
+ command 'grid list' do |c|
9
+ c.syntax = 'kontena grid list'
10
+ c.description = 'List all grids'
11
+ c.action do |args, options|
12
+ Kontena::Cli::Grids::Grids.new.list
13
+ end
14
+ end
15
+
16
+ command 'grid use' do |c|
17
+ c.syntax = 'kontena grid use <name>'
18
+ c.description = 'Switch to use specific grid'
19
+ c.action do |args, options|
20
+ raise ArgumentError.new('grid name is required. For a list of existing grids please run: kontena grid list') if args[0].nil?
21
+ Kontena::Cli::Grids::Grids.new.use(args[0])
22
+ end
23
+ end
24
+
25
+ command 'grid show' do |c|
26
+ c.syntax = 'kontena grid show <name>'
27
+ c.description = 'Show grid details'
28
+ c.action do |args, options|
29
+ raise ArgumentError.new('grid name is required. For a list of existing grids please run: kontena grid list') if args[0].nil?
30
+ Kontena::Cli::Grids::Grids.new.show(args[0])
31
+ end
32
+ end
33
+
34
+ command 'grid current' do |c|
35
+ c.syntax = 'kontena grid current'
36
+ c.description = 'Show current grid details'
37
+ c.action do |args, options|
38
+ Kontena::Cli::Grids::Grids.new.current
39
+ end
40
+ end
41
+
42
+ command 'grid audit-log' do |c|
43
+ c.syntax = 'kontena grid audit-log'
44
+ c.description = 'Show audit log of the current grid'
45
+ c.option '-l', '--limit INTEGER', Integer, 'Number of lines'
46
+ c.action do |args, options|
47
+ Kontena::Cli::Grids::AuditLog.new.show(options)
48
+ end
49
+ end
50
+
51
+ command 'grid create' do |c|
52
+ c.syntax = 'kontena grid create <name>'
53
+ c.description = 'Create a new grid'
54
+ c.action do |args, options|
55
+ Kontena::Cli::Grids::Grids.new.create(args[0])
56
+ end
57
+ end
58
+
59
+ command 'grid remove' do |c|
60
+ c.syntax = 'kontena grid remove <name>'
61
+ c.description = 'Removes grid'
62
+ c.action do |args, options|
63
+ Kontena::Cli::Platform::Grids.new.destroy(args[0])
64
+ end
65
+ end
66
+
67
+ command 'grid list-users' do |c|
68
+ c.syntax = 'kontena grid list-users'
69
+ c.description = 'Show grid users'
70
+ c.action do |args, options|
71
+ Kontena::Cli::Grids::Users.new.list
72
+ end
73
+ end
74
+
75
+ command 'grid add-user' do |c|
76
+ c.syntax = 'kontena grid add-user <email>'
77
+ c.description = 'Assign user to grid'
78
+ c.action do |args, options|
79
+ Kontena::Cli::Grids::Users.new.add(args[0])
80
+ end
81
+ end
82
+
83
+ command 'grid remove-user' do |c|
84
+ c.syntax = 'kontena grid remove-user <email>'
85
+ c.description = 'Unassign user from grid'
86
+ c.action do |args, options|
87
+ Kontena::Cli::Grids::Users.new.remove(args[0])
88
+ end
89
+ end
@@ -0,0 +1,105 @@
1
+ require 'kontena/client'
2
+ require_relative '../common'
3
+
4
+ module Kontena::Cli::Grids
5
+ class Grids
6
+ include Kontena::Cli::Common
7
+
8
+ def list
9
+ require_api_url
10
+
11
+ if grids['grids'].size == 0
12
+ print color("You don't have any grids yet. Create first one with 'kontena grids create' command", :yellow)
13
+ end
14
+
15
+ puts '%-30.30s %-10s %-10s %-10s' % ['Name', 'Nodes', 'Containers', 'Users']
16
+ grids['grids'].each do |grid|
17
+ if grid['id'] == current_grid
18
+ name = "#{grid['name']} *"
19
+ else
20
+ name = grid['name']
21
+ end
22
+ puts '%-30.30s %-10s %-10s %-10s' % [name, grid['nodeCount'], grid['containerCount'], grid['userCount']]
23
+ end
24
+ end
25
+
26
+ def use(name)
27
+ require_api_url
28
+
29
+ grid = find_grid_by_name(name)
30
+ if !grid.nil?
31
+ self.current_grid = grid
32
+ print color("Using #{grid['name']}", :green)
33
+ else
34
+ print color('Could not resolve grid by name. For a list of existing grids please run: kontena grid list', :red)
35
+ end
36
+
37
+ end
38
+
39
+ def show(name)
40
+ require_api_url
41
+
42
+ grid = find_grid_by_name(name)
43
+ print_grid(grid)
44
+
45
+ end
46
+
47
+ def current
48
+ require_api_url
49
+ if current_grid.nil?
50
+ puts 'No grid selected. To select grid, please run: kontena use <grid name>'
51
+
52
+ else
53
+ grid = client(require_token).get("grids/#{current_grid}")
54
+ print_grid(grid)
55
+ end
56
+ end
57
+
58
+ def create(name=nil)
59
+ require_api_url
60
+
61
+ token = require_token
62
+ payload = {
63
+ name: name
64
+ }
65
+ grid = client(token).post('grids', payload)
66
+ puts "created #{grid['name']} (#{grid['id']})" if grid
67
+ end
68
+
69
+ def destroy(name)
70
+ require_api_url
71
+
72
+ grid = find_grid_by_name(name)
73
+
74
+ if !grid.nil?
75
+ response = client(token).delete("grids/#{grid['id']}")
76
+ if response
77
+ clear_current_grid if grid['id'] == current_grid
78
+ puts "removed #{grid['name']} (#{grid['id']})"
79
+ end
80
+ else
81
+ print color('Could not resolve grid by name. For a list of existing grids please run: kontena grid list', :red)
82
+ end
83
+ end
84
+
85
+ private
86
+
87
+ ##
88
+ # @param [Hash] grid
89
+ def print_grid(grid)
90
+ puts "#{grid['name']}:"
91
+ puts " token: #{grid['token']}"
92
+ puts " users: #{grid['userCount']}"
93
+ puts " nodes: #{grid['nodeCount']}"
94
+ puts " containers: #{grid['containerCount']}"
95
+ end
96
+
97
+ def grids
98
+ @grids ||= client(require_token).get('grids')
99
+ end
100
+
101
+ def find_grid_by_name(name)
102
+ grids['grids'].find {|grid| grid['name'] == name }
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,32 @@
1
+ require 'kontena/client'
2
+ require_relative '../common'
3
+
4
+ module Kontena::Cli::Grids
5
+ class Users
6
+ include Kontena::Cli::Common
7
+
8
+ def add(email)
9
+ require_api_url
10
+ token = require_token
11
+ data = { email: email }
12
+ client(token).post("grids/#{current_grid}/users", data)
13
+ end
14
+
15
+ def remove(email)
16
+ require_api_url
17
+ token = require_token
18
+ result = client(token).delete("grids/#{current_grid}/users/#{email}")
19
+ end
20
+
21
+ def list
22
+ require_api_url
23
+ token = require_token
24
+ result = client(token).get("grids/#{current_grid}/users")
25
+ puts "%-40s %-40s" % ['Email', 'Name']
26
+ result['users'].each { |user|
27
+ puts "%-40.40s %-40.40s" % [user['email'], user['name']]
28
+ }
29
+ end
30
+
31
+ end
32
+ end
@@ -0,0 +1,27 @@
1
+ module Kontena::Cli::Nodes; end;
2
+
3
+ require_relative 'nodes'
4
+
5
+ command 'node list' do |c|
6
+ c.syntax = 'kontena node list'
7
+ c.description = 'List all nodes'
8
+ c.action do |args, options|
9
+ Kontena::Cli::Nodes::Nodes.new.list
10
+ end
11
+ end
12
+
13
+ command 'node show' do |c|
14
+ c.syntax = 'kontena node show'
15
+ c.description = 'Show node details'
16
+ c.action do |args, options|
17
+ Kontena::Cli::Nodes::Nodes.new.show(args[0])
18
+ end
19
+ end
20
+
21
+ command 'node remove' do |c|
22
+ c.syntax = 'kontena node remove'
23
+ c.description = 'Remove node'
24
+ c.action do |args, options|
25
+ Kontena::Cli::Nodes::Nodes.new.destroy(args[0])
26
+ end
27
+ end
@@ -0,0 +1,61 @@
1
+ require 'kontena/client'
2
+ require_relative '../common'
3
+
4
+ module Kontena::Cli::Nodes
5
+ class Nodes
6
+ include Kontena::Cli::Common
7
+
8
+ def list
9
+ require_api_url
10
+ require_current_grid
11
+ token = require_token
12
+
13
+ grids = client(token).get("grids/#{current_grid}/nodes")
14
+ puts "%-20s %-20s %-10s %-20s %-10s" % ['Name', 'OS', 'Driver', 'Labels', 'Status']
15
+ grids['nodes'].each do |node|
16
+ if node['connected']
17
+ status = 'online'
18
+ else
19
+ status = 'offline'
20
+ end
21
+ puts "%-20.20s %-20.20s %-10s %-20.20s %-10s" % [
22
+ node['name'],
23
+ node['os'],
24
+ node['driver'],
25
+ (node['labels'] || ['-']).join(","),
26
+ status
27
+ ]
28
+ end
29
+ end
30
+
31
+ def show(id)
32
+ require_api_url
33
+ require_current_grid
34
+ token = require_token
35
+
36
+ node = client(token).get("grids/#{current_grid}/nodes/#{id}")
37
+ puts "#{node['name']}:"
38
+ puts " id: #{node['id']}"
39
+ puts " connected: #{node['connected'] ? 'yes': 'no'}"
40
+ puts " last connect: #{node['updated_at']}"
41
+ puts " os: #{node['os']}"
42
+ puts " driver: #{node['driver']}"
43
+ puts " kernel: #{node['kernel_version']}"
44
+ puts " cpus: #{node['cpus']}"
45
+ puts " memory: #{node['mem_total'] / 1024 / 1024}M"
46
+ puts " labels:"
47
+ if node['labels']
48
+ node['labels'].each{|l| puts " - #{l}"}
49
+ end
50
+ end
51
+
52
+ def destroy(id)
53
+ require_api_url
54
+ require_current_grid
55
+ token = require_token
56
+
57
+ node = client(token).delete("grids/#{current_grid}/nodes/#{id}")
58
+ end
59
+
60
+ end
61
+ end
@@ -0,0 +1,92 @@
1
+ module Kontena::Cli::Server; end;
2
+
3
+ require_relative 'server'
4
+ require_relative 'user'
5
+
6
+ command 'connect' do |c|
7
+ c.syntax = 'kontena connect <url>'
8
+ c.description = 'Connect to Kontena server'
9
+ c.action do |args, options|
10
+ Kontena::Cli::Server::Server.new.connect(args[0], options)
11
+ end
12
+ end
13
+
14
+ command 'disconnect' do |c|
15
+ c.syntax = 'kontena disconnect'
16
+ c.description = 'Disconnect from Kontena server'
17
+ c.action do |args, options|
18
+ Kontena::Cli::Server::Server.new.disconnect
19
+ end
20
+ end
21
+
22
+ command 'login' do |c|
23
+ c.syntax = 'kontena login'
24
+ c.description = 'Login to Kontena server'
25
+ c.action do |args, options|
26
+ Kontena::Cli::Server::User.new.login
27
+ end
28
+ end
29
+
30
+ command 'logout' do |c|
31
+ c.syntax = 'kontena logout'
32
+ c.description = 'Logout from Kontena server'
33
+ c.action do |args, options|
34
+ Kontena::Cli::Server::User.new.logout
35
+ end
36
+ end
37
+
38
+ command 'whoami' do |c|
39
+ c.syntax = 'kontena whoami'
40
+ c.description = 'Display your Kontena email address and server url'
41
+ c.action do |args, options|
42
+ Kontena::Cli::Server::User.new.whoami
43
+ end
44
+ end
45
+
46
+ command 'register' do |c|
47
+ c.syntax = 'kontena register'
48
+ c.description = 'Register to Kontena.io'
49
+ c.action do |args, options|
50
+ Kontena::Cli::Server::User.new.register
51
+ end
52
+ end
53
+
54
+ command 'verify account' do |c|
55
+ c.syntax = 'kontena verify account <token>'
56
+ c.description = 'Verify Kontena.io account'
57
+ c.action do |args, options|
58
+ Kontena::Cli::Server::User.new.verify_account(args[0])
59
+ end
60
+ end
61
+
62
+ command 'invite' do |c|
63
+ c.syntax = 'kontena invite <email>'
64
+ c.description = 'Invite user to Kontena server'
65
+ c.action do |args, options|
66
+ Kontena::Cli::Server::User.new.invite(args[0])
67
+ end
68
+ end
69
+
70
+ command 'forgot password' do |c|
71
+ c.syntax = 'kontena forgot password <email>'
72
+ c.description = 'Request password reset for Kontena account'
73
+ c.action do |args, options|
74
+ Kontena::Cli::Server::User.new.request_password_reset(args[0])
75
+ end
76
+ end
77
+
78
+ command 'reset password' do |c|
79
+ c.syntax = 'kontena reset password <token>'
80
+ c.description = 'Reset Kontena password'
81
+ c.action do |args, options|
82
+ Kontena::Cli::Server::User.new.reset_password(args[0])
83
+ end
84
+ end
85
+
86
+ command 'registry add' do |c|
87
+ c.syntax = 'kontena registry add'
88
+ c.description = 'Add registry information'
89
+ c.action do |args, options|
90
+ Kontena::Cli::Server::User.new.add_registry
91
+ end
92
+ end
@@ -0,0 +1,45 @@
1
+ require 'kontena/client'
2
+ require_relative '../common'
3
+
4
+ module Kontena::Cli::Server
5
+ class Server
6
+ include Kontena::Cli::Common
7
+
8
+ def connect(api_url = nil, options)
9
+ until !api_url.nil? && !api_url.empty?
10
+ api_url = ask('Kontena server url: ')
11
+ end
12
+ inifile['server']['url'] = api_url
13
+ inifile.save(filename: ini_filename)
14
+
15
+ sleep 0.1
16
+ if client.get('ping') # test server connection
17
+ display_logo
18
+ else
19
+ print color('Could not connect to server', :red)
20
+ end
21
+
22
+ end
23
+
24
+ def disconnect
25
+ inifile['server'].delete('url')
26
+ inifile.save(filename: ini_filename)
27
+ end
28
+
29
+ private
30
+ def display_logo
31
+ logo = <<LOGO
32
+ _ _
33
+ | | _____ _ __ | |_ ___ _ __ __ _
34
+ | |/ / _ \\| '_ \\| __/ _ \\ '_ \\ / _` |
35
+ | < (_) | | | | || __/ | | | (_| |
36
+ |_|\\_\\___/|_| |_|\\__\\___|_| |_|\\__,_|
37
+ -------------------------------------
38
+ Copyright (c)2015 Kontena, Inc.
39
+
40
+ LOGO
41
+ puts logo
42
+ end
43
+
44
+ end
45
+ end