fenton_shell 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
File without changes
@@ -0,0 +1,15 @@
1
+ require 'excon'
2
+ require 'json'
3
+ require 'fileutils'
4
+ require 'sshkey'
5
+ require 'highline/import'
6
+
7
+ require 'fenton_shell/version'
8
+
9
+ require 'fenton_shell/config_file'
10
+ require 'fenton_shell/key'
11
+
12
+ require 'fenton_shell/organization'
13
+ require 'fenton_shell/client'
14
+ require 'fenton_shell/project'
15
+ require 'fenton_shell/certificate'
File without changes
@@ -0,0 +1,83 @@
1
+ module FentonShell
2
+ # Interfaces with the certificate api on fenton server
3
+ class Certificate
4
+ # @!attribute [r] message
5
+ # @return [String] success or failure message and why
6
+ attr_accessor :message
7
+
8
+ # Creates a new certificate on fenton server by sending a post
9
+ # request with json from the command line to create the certificate
10
+ #
11
+ # @param global_options [Hash] global command line options
12
+ # @param options [Hash] json fields to send to fenton server
13
+ # @return [String] success or failure message
14
+ def create(global_options, options)
15
+ status, body = certificate_create(global_options, options)
16
+
17
+ if status == 201
18
+ save_message('Certificate': ['created!'])
19
+ true
20
+ else
21
+ save_message(body)
22
+ false
23
+ end
24
+ end
25
+
26
+ private
27
+
28
+ # Sends a post request with json from the command line certificate
29
+ #
30
+ # @param global_options [Hash] global command line options
31
+ # @param options [Hash] json fields to send to fenton server
32
+ # @return [Fixnum] http status code
33
+ # @return [String] message back from fenton server
34
+ def certificate_create(global_options, options)
35
+ result = Excon.post(
36
+ "#{global_options[:fenton_server_url]}/certificates.json",
37
+ body: certificate_json(options),
38
+ headers: { 'Content-Type' => 'application/json' }
39
+ )
40
+
41
+ write_client_certificate(
42
+ public_key_cert_location(options[:public_key]),
43
+ JSON.parse(result.body)['data']['attributes']['certificate']
44
+ )
45
+
46
+ [result.status, JSON.parse(result.body)]
47
+ end
48
+
49
+ def write_client_certificate(file_path, content)
50
+ File.write(file_path, content)
51
+ File.chmod(0o600, file_path)
52
+ end
53
+
54
+ def public_key_cert_location(public_key_location)
55
+ public_key_location.gsub(%r{\.pub}, '-cert.pub')
56
+ end
57
+
58
+ # Formulates the certificate json for the post request
59
+ #
60
+ # @param options [Hash] fields from fenton command line
61
+ # @return [String] json created from the options hash
62
+ def certificate_json(options)
63
+ {
64
+ certificate: {
65
+ client: options[:client],
66
+ project: options[:project]
67
+ }
68
+ }.to_json
69
+ end
70
+
71
+ # Helps output the error message from fenton server
72
+ #
73
+ # @param msg [Hash] fields from fenton public classes
74
+ # @return [String] changed hash fields to string for command line output
75
+ def save_message(msg = {})
76
+ self.message ||= ''
77
+
78
+ msg.each do |key, value|
79
+ self.message << "#{key.capitalize} #{value.first}\n"
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,92 @@
1
+ module FentonShell
2
+ # Interfaces with the client api on fenton server
3
+ class Client
4
+ # @!attribute [r] message
5
+ # @return [String] success or failure message and why
6
+ attr_accessor :message
7
+
8
+ # Creates a new client on fenton server by sending a post
9
+ # request with json from the command line to create the client
10
+ #
11
+ # @param global_options [Hash] global command line options
12
+ # @param options [Hash] json fields to send to fenton server
13
+ # @return [String] success or failure message
14
+
15
+ def create(global_options, options)
16
+ status, body = client_create(global_options, options)
17
+
18
+ if status == 201
19
+ save_message('Client': ['created!'])
20
+ true
21
+ else
22
+ save_message(body)
23
+ false
24
+ end
25
+ end
26
+
27
+ # Calls create new client then creates an organization for that client
28
+ # via a post request with json from the command line
29
+ #
30
+ # @param global_options [Hash] global command line options
31
+ # @param options [Hash] json fields to send to fenton server
32
+ # @return [String] success or failure message
33
+
34
+ def create_with_organization(global_options, options)
35
+ create(global_options, options)
36
+
37
+ if Organization.new.create(global_options, name: options[:username],
38
+ key: options[:username])
39
+ save_message('Organization': ['created!'])
40
+ true
41
+ else
42
+ save_message('Organization': ['not created!'])
43
+ false
44
+ end
45
+ end
46
+
47
+ private
48
+
49
+ # Sends a post request with json from the command line client
50
+ #
51
+ # @param global_options [Hash] global command line options
52
+ # @param options [Hash] json fields to send to fenton server
53
+ # @return [Fixnum] http status code
54
+ # @return [String] message back from fenton server
55
+ def client_create(global_options, options)
56
+ result = Excon.post(
57
+ "#{global_options[:fenton_server_url]}/clients.json",
58
+ body: client_json(options),
59
+ headers: { 'Content-Type' => 'application/json' }
60
+ )
61
+
62
+ [result.status, JSON.parse(result.body)]
63
+ end
64
+
65
+ # Formulates the client json for the post request
66
+ #
67
+ # @param options [Hash] fields from fenton command line
68
+ # @return [String] json created from the options hash
69
+ def client_json(options)
70
+ {
71
+ client: {
72
+ username: options[:username],
73
+ name: options[:name],
74
+ email: options[:email],
75
+ public_key: File.read(options[:public_key])
76
+ }
77
+ }.to_json
78
+ end
79
+
80
+ # Helps output the error message from fenton server
81
+ #
82
+ # @param msg [Hash] fields from fenton public classes
83
+ # @return [String] changed hash fields to string for command line output
84
+ def save_message(msg = {})
85
+ self.message ||= ''
86
+
87
+ msg.each do |key, value|
88
+ self.message << "#{key.capitalize} #{value.first}\n"
89
+ end
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,122 @@
1
+ module FentonShell
2
+ # Provides a local configuration file
3
+ class ConfigFile
4
+ # @!attribute [r] message
5
+ # @return [String] success or failure message and why
6
+ attr_accessor :message
7
+
8
+ # Creates a configuration file on the local file system
9
+ #
10
+ # @param global_options [Hash] global command line options
11
+ # @param options [Hash] fields to send to create the configuration file
12
+ # @return [String] success or failure message
13
+
14
+ def create(global_options, options)
15
+ status, body = config_file_create(global_options, options)
16
+
17
+ if status
18
+ save_message('ConfigFile': ['created!'])
19
+ true
20
+ else
21
+ save_message(body)
22
+ false
23
+ end
24
+ end
25
+
26
+ class << self
27
+ # Responds with the default organization
28
+ #
29
+ # @param global_options [Hash] global command line options
30
+ # @return [String] default organization key for client
31
+ def default_organization(global_options)
32
+ config_file(global_options)[:default_organization]
33
+ end
34
+
35
+ # Responds with the default username
36
+ #
37
+ # @param global_options [Hash] global command line options
38
+ # @return [String] default username for client
39
+ def username(global_options)
40
+ config_file(global_options)[:username]
41
+ end
42
+
43
+ # Responds with the default public key
44
+ #
45
+ # @param global_options [Hash] global command line options
46
+ # @return [String] default public key for client
47
+ def public_key(global_options)
48
+ config_file(global_options)[:public_key]
49
+ end
50
+
51
+ private
52
+
53
+ # Loads the configuration file content
54
+ #
55
+ # @param global_options [Hash] global command line options
56
+ # @return [Hash] content from yaml config file
57
+ def config_file(global_options)
58
+ YAML.load_file("#{global_options[:directory]}/config")
59
+ end
60
+ end
61
+
62
+ private
63
+
64
+ # Creates the configuration file
65
+ #
66
+ # @param options [Hash] fields from fenton command line
67
+ # @return [Object] true or false
68
+ # @return [String] message on success or failure
69
+ def config_file_create(global_options, options)
70
+ config_directory_create(global_options)
71
+
72
+ file = "#{global_options[:directory]}/config"
73
+ options.store(:fenton_server_url, global_options[:fenton_server_url])
74
+ content = config_generation(options)
75
+ File.write(file, content)
76
+
77
+ [true, 'ConfigFile': ['created!']]
78
+ end
79
+
80
+ # Generates the configuration file content
81
+ #
82
+ # @param options [Hash] fields from fenton command line
83
+ # @return [String] true or false
84
+ def config_generation(options)
85
+ config_contents = {}
86
+
87
+ config_options = options.keys.map(&:to_sym).sort.uniq
88
+ config_options.delete(:password)
89
+ config_options.each do |config_option|
90
+ config_contents.store(config_option.to_sym, options[config_option])
91
+ end
92
+
93
+ config_contents.store(:default_organization, options[:username])
94
+
95
+ config_contents.to_yaml
96
+ end
97
+
98
+ def config_directory_create(global_options)
99
+ FileUtils.mkdir_p(global_options[:directory])
100
+ rescue Errno::EEXIST => e
101
+ if e.message =~ %r{File exists}
102
+ save_message('ConfigFile': ['creation failed, a file ' \
103
+ 'exists in that path'])
104
+ return
105
+ end
106
+
107
+ raise e.message
108
+ end
109
+
110
+ # Helps output the error message from the class
111
+ #
112
+ # @param msg [Hash] fields from fenton public classes
113
+ # @return [String] changed hash fields to string for command line output
114
+ def save_message(msg = {})
115
+ self.message ||= ''
116
+
117
+ msg.each do |key, value|
118
+ self.message << "#{key.capitalize} #{value.first}\n"
119
+ end
120
+ end
121
+ end
122
+ end
@@ -0,0 +1,71 @@
1
+ module FentonShell
2
+ # Provides SSH Key generation on the local client
3
+ class Key
4
+ # @!attribute [r] message
5
+ # @return [String] success or failure message and why
6
+ attr_accessor :message
7
+
8
+ # Creates a new key on the local client
9
+ #
10
+ # @param options [Hash] fields to send to ssh key pair generation
11
+ # @return [String] success or failure message
12
+
13
+ def create(options)
14
+ status, body = key_create(options)
15
+
16
+ if status
17
+ save_message('Key': ['created!'])
18
+ true
19
+ else
20
+ save_message(body)
21
+ false
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ # Sends a post request with json from the command line key
28
+ #
29
+ # @param options [Hash] fields from fenton command line
30
+ # @return [Object] true or false
31
+ # @return [String] message on success or failure
32
+ def key_create(options)
33
+ ssh_key = key_generation(options)
34
+ ssh_private_key_file = options[:private_key]
35
+ ssh_public_key_file = "#{ssh_private_key_file}.pub"
36
+
37
+ # TODO: - add to .fenton/config file
38
+ File.write(ssh_private_key_file, ssh_key.private_key)
39
+ File.chmod(0o600, ssh_private_key_file)
40
+ File.write(ssh_public_key_file, ssh_key.ssh_public_key)
41
+ File.chmod(0o600, ssh_public_key_file)
42
+
43
+ [true, 'Key': ['creation failed']]
44
+ end
45
+
46
+ # Generates the SSH key pair
47
+ #
48
+ # @param options [Hash] fields from fenton command line
49
+ # @return [String] ssh key pair object created from the options hash
50
+ def key_generation(options)
51
+ SSHKey.generate(
52
+ type: options[:type],
53
+ bits: options[:bits],
54
+ comment: 'ssh@fenton_shell',
55
+ passphrase: options[:passphrase]
56
+ )
57
+ end
58
+
59
+ # Helps output the error message from fenton server
60
+ #
61
+ # @param msg [Hash] fields from fenton public classes
62
+ # @return [String] changed hash fields to string for command line output
63
+ def save_message(msg = {})
64
+ self.message ||= ''
65
+
66
+ msg.each do |key, value|
67
+ self.message << "#{key.capitalize} #{value.first}\n"
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,70 @@
1
+ module FentonShell
2
+ # Interfaces with the organization api on fenton server
3
+ class Organization
4
+ # @!attribute [r] message
5
+ # @return [String] success or failure message and why
6
+ attr_accessor :message
7
+
8
+ # Creates a new organization on fenton server by sending a post
9
+ # request with json from the command line to create the organization
10
+ #
11
+ # @param global_options [Hash] global command line options
12
+ # @param options [Hash] json fields to send to fenton server
13
+ # @return [String] success or failure message
14
+
15
+ def create(global_options, options)
16
+ status, body = organization_create(global_options, options)
17
+
18
+ if status == 201
19
+ save_message('Organization': ['created!'])
20
+ true
21
+ else
22
+ save_message(body)
23
+ false
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ # Sends a post request with json from the command line organization
30
+ #
31
+ # @param global_options [Hash] global command line options
32
+ # @param options [Hash] json fields to send to fenton server
33
+ # @return [Fixnum] http status code
34
+ # @return [String] message back from fenton server
35
+ def organization_create(global_options, options)
36
+ result = Excon.post(
37
+ "#{global_options[:fenton_server_url]}/organizations.json",
38
+ body: organization_json(options),
39
+ headers: { 'Content-Type' => 'application/json' }
40
+ )
41
+
42
+ [result.status, JSON.parse(result.body)]
43
+ end
44
+
45
+ # Formulates the organization json for the post request
46
+ #
47
+ # @param options [Hash] fields from fenton command line
48
+ # @return [String] json created from the options hash
49
+ def organization_json(options)
50
+ {
51
+ organization: {
52
+ name: options[:name],
53
+ key: options[:key]
54
+ }
55
+ }.to_json
56
+ end
57
+
58
+ # Helps output the error message from fenton server
59
+ #
60
+ # @param msg [Hash] fields from fenton public classes
61
+ # @return [String] changed hash fields to string for command line output
62
+ def save_message(msg = {})
63
+ self.message ||= ''
64
+
65
+ msg.each do |key, value|
66
+ self.message << "#{key.capitalize} #{value.first}\n"
67
+ end
68
+ end
69
+ end
70
+ end