meroku 2.0.3 → 2.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 74fd11997f53c773598e76575486b10572ce0acc
4
- data.tar.gz: 901942c1d9a97b7095c26a6bda871661c6781ee5
3
+ metadata.gz: 1f5b77a7020b9070c98356c545c6bad3c10cb41f
4
+ data.tar.gz: 245195d10ba32e577c050181618cabe13a9a3994
5
5
  SHA512:
6
- metadata.gz: def75bd1beabc9fd05db76c4dd75bce0bcb6422b844ce38887287b4ce25a4e79958ba5f59d8df04aba7c5fca59d8b341e2ab6eee597a83fb2ffa1f710d08797d
7
- data.tar.gz: 72d8f5f166cb5c546cf5c9da204a4578a382709d9f4da0d08692fb066c1bf1717f8db3ad660dd70f81d7f776ea0625b8673139b9b62a77f3d4dc4826164b5e87
6
+ metadata.gz: 4d9f6674d08add9d0fc57b5b9d8246c7797dfe622b9cf5cbbca2da3b08e95e48dc0410e994c6cc5364cad45c87d5cbf9db37c7f5db5c3cda6f81e7a7cb94cc7b
7
+ data.tar.gz: 9269502bb855e2cc21526a177bd25832ad1ae83334b65a0699a1092d05ad7cc2baaaed0be6d127cb0bd96f34acc044285e25304d4a987e72f82d6eca188fdafb
data/bin/meroku CHANGED
@@ -3,4 +3,4 @@
3
3
  $LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'
4
4
  require 'meroku'
5
5
 
6
- Meroku::CLI.start(*ARGV)
6
+ Meroku::CLI::Session.new(*ARGV)
@@ -32,6 +32,7 @@ class PublickeysController < ApplicationController
32
32
  title: 'Invalid Authorization Token',
33
33
  detail: 'Invalid token. Try logging in again.'
34
34
  }]) and return
35
+ end
35
36
 
36
37
  respond_to do |format|
37
38
  if @publickey.save
@@ -1,3 +1,14 @@
1
1
  class Publickey < ApplicationRecord
2
2
  belongs_to :user
3
+
4
+ # Updates /home/git/.ssh/authorized_keys
5
+ # called when something added/?/etc to publickeys table
6
+ def self.refresh
7
+ Rails.logger.debug "DB8 publickey.rb:def self.refresh called"
8
+ Rails.logger.debug "wipwip"
9
+ end
10
+
11
+ def after_save
12
+ Publickey.refresh
13
+ end
3
14
  end
@@ -8,9 +8,11 @@ require "byebug"
8
8
  require 'rest-client'
9
9
  require "meroku/version"
10
10
  require "meroku/tunnel"
11
- require "meroku/cli"
12
- require "meroku/aws"
11
+ require "meroku/cli/user"
12
+ require "meroku/cli/admin_user"
13
13
  require "meroku/infrastructure"
14
+ require "meroku/cli"
15
+
14
16
 
15
17
  module Meroku
16
18
 
@@ -1,7 +1,10 @@
1
+ require "meroku/cli/session"
2
+
1
3
  module Meroku
2
4
  module CLI
3
5
 
4
- HELP = <<~HEREDOC
6
+ def help
7
+ <<~HEREDOC
5
8
  Usage: meroku command subcommand
6
9
 
7
10
  Examples
@@ -14,38 +17,38 @@ module Meroku
14
17
 
15
18
  meroku infrastrucuture spawn # Spawns server
16
19
 
17
- HEREDOC
18
-
19
- def self.start(*args)
20
- case args.join(" ")
21
- when "infrastructure spawn"
22
- load_secrets || exit
23
- node = Meroku::Infrastructure::Node.new.associate_address.install_packages.install_frontend_app
24
- puts "spawned #{node.instance.try(:instance_id)}"
25
- when "infrastructure despawn"
26
- load_secrets || exit
27
- Meroku::Infrastructure.despawn
28
- when "signup"
29
- signup
30
- when "keys:add"
31
- token_check || exit
32
- keys_add
33
- when "create"
34
- token_check || exit
35
- create
36
- else
37
- puts HELP
38
- end
20
+ HEREDOC
39
21
  end
40
-
41
- def self.signup
22
+
23
+ #def self.start(*args)
24
+ # case args.join(" ")
25
+ # when "infrastructure spawn"
26
+ # load_secrets || exit
27
+ # node = Meroku::Infrastructure::Node.new.associate_address.install_packages.install_frontend_app
28
+ # puts "spawned #{node.instance.try(:instance_id)}"
29
+ # when "infrastructure despawn"
30
+ # load_secrets || exit
31
+ # Meroku::Infrastructure.despawn
32
+ # when "signup"
33
+ # signup
34
+ # when "keys:add"
35
+ # token_check || exit
36
+ # keys_add
37
+ # when "create"
38
+ # token_check || exit
39
+ # create
40
+ # else
41
+ # puts HELP
42
+ # end
43
+ #end
44
+
45
+ def signup
42
46
  print "Email: "
43
47
  email = STDIN.gets
44
48
  print "Password: "
45
49
  password = STDIN.noecho(&:gets).chomp
46
50
  print "\n"
47
51
  url = "https://www.meroku.com/users.json"
48
- #url = "http://localhost:3000/users.json"
49
52
  response_json = RestClient.post url, {:user=>{:email => email, :password => password, :password_confirmation => password}}.to_json, timeout: 1, :content_type => :json, :accept => :json
50
53
  if JSON.parse(response_json)["errors"] && JSON.parse(response_json)["errors"].size > 0
51
54
  puts JSON.parse(response_json)["errors"].map{|x| x["detail"]}.join(".")
@@ -53,11 +56,33 @@ module Meroku
53
56
  email = JSON.parse(response_json)["data"]["attributes"]["email"]
54
57
  token = JSON.parse(response_json)["data"]["attributes"]["token"]
55
58
  puts "Signed up #{email}"
56
- save_token(token)
59
+ dirname = File.dirname("#{Dir.home}/.meroku")
60
+ FileUtils.mkdir(dirname) if !File.directory?(dirname)
61
+ File.open("#{Dir.home}/.meroku/.token", 'w') { |file| file.write(token) }
57
62
  end
58
63
  end
59
64
 
60
- def self.create
65
+ def keys_add(session)
66
+ if !File.exist? "#{Dir.home}/.ssh/id_rsa.pub"
67
+ puts "error: File #{Dir.home}/.ssh/id_rsa.pub not found"
68
+ puts "You can use this command to generate a key:"
69
+ puts " ssh-keygen -t rsa"
70
+ return nil
71
+ end
72
+ name = "id_rsa.pub"
73
+ data = `cat ~/.ssh/id_rsa.pub`.chomp
74
+ url = "https://www.meroku.com/publickeys.json"
75
+ response_json = RestClient.post url, {:publickey=>{:name => name, :data=>data}, :token=>session.token}.to_json, timeout: 1, :content_type => :json, :accept => :json
76
+
77
+ if JSON.parse(response_json)["errors"] && JSON.parse(response_json)["errors"].size > 0
78
+ puts JSON.parse(response_json)["errors"].map{|x| x["detail"]}.join(".")
79
+ else
80
+ name = JSON.parse(response_json)["data"]["attributes"]["name"]
81
+ puts "Added #{name}"
82
+ end
83
+ end
84
+
85
+ def create
61
86
  url = "https://www.meroku.com/apps.json"
62
87
  token = `cat ~/.meroku/.token`.chomp
63
88
  response_json = RestClient.post url, {:app=>{:name => "unnamed"}, :token=>token}.to_json, timeout: 1, :content_type => :json, :accept => :json
@@ -96,34 +121,28 @@ module Meroku
96
121
  Dotenv.load(env_file)
97
122
  end
98
123
 
99
- def self.save_token(token)
100
- dirname = File.dirname("#{Dir.home}/.meroku")
101
- unless File.directory?(dirname)
102
- FileUtils.mkdir(dirname)
103
- end
104
- File.open("#{Dir.home}/.meroku/.token", 'w') { |file| file.write(token) }
105
- end
106
124
 
107
- def self.keys_add
108
- if !File.exist? "#{Dir.home}/.ssh/id_rsa.pub"
109
- puts "error: File #{Dir.home}/.ssh/id_rsa.pub not found"
110
- puts "You can use this command to generate a key:"
111
- puts " ssh-keygen -t rsa"
112
- return nil
113
- end
114
- name = "id_rsa.pub"
115
- data = `cat ~/.ssh/id_rsa.pub`.chomp
116
- url = "https://www.meroku.com/publickeys.json"
117
- token = `cat ~/.meroku/.token`.chomp
118
- response_json = RestClient.post url, {:publickey=>{:name => name, :data=>data}, :token=>token}.to_json, timeout: 1, :content_type => :json, :accept => :json
119
125
 
120
- if JSON.parse(response_json)["errors"] && JSON.parse(response_json)["errors"].size > 0
121
- puts JSON.parse(response_json)["errors"].map{|x| x["detail"]}.join(".")
122
- else
123
- name = JSON.parse(response_json)["data"]["attributes"]["name"]
124
- puts "Added #{name}"
125
- end
126
- end
126
+ #def self.keys_add
127
+ # if !File.exist? "#{Dir.home}/.ssh/id_rsa.pub"
128
+ # puts "error: File #{Dir.home}/.ssh/id_rsa.pub not found"
129
+ # puts "You can use this command to generate a key:"
130
+ # puts " ssh-keygen -t rsa"
131
+ # return nil
132
+ # end
133
+ # name = "id_rsa.pub"
134
+ # data = `cat ~/.ssh/id_rsa.pub`.chomp
135
+ # url = "https://www.meroku.com/publickeys.json"
136
+ # token = `cat ~/.meroku/.token`.chomp
137
+ # response_json = RestClient.post url, {:publickey=>{:name => name, :data=>data}, :token=>token}.to_json, timeout: 1, :content_type => :json, :accept => :json
138
+ #
139
+ # if JSON.parse(response_json)["errors"] && JSON.parse(response_json)["errors"].size > 0
140
+ # puts JSON.parse(response_json)["errors"].map{|x| x["detail"]}.join(".")
141
+ # else
142
+ # name = JSON.parse(response_json)["data"]["attributes"]["name"]
143
+ # puts "Added #{name}"
144
+ # end
145
+ #end
127
146
 
128
147
  end
129
148
 
@@ -0,0 +1,20 @@
1
+ module Meroku
2
+ module CLI
3
+
4
+ module AdminUser
5
+
6
+ def self.load_secrets(obj)
7
+ env_file = Dir.home+'/.meroku/meroku.env'
8
+ if !File.exist?(env_file)
9
+ puts "error: File not found (#{Dir.home}/.meroku/meroku.env)"
10
+ return nil
11
+ end
12
+ Dotenv.load(env_file)
13
+ obj.ec2_client = ::Aws::EC2::Client.new(
14
+ region: 'us-east-1',
15
+ credentials: ::Aws::Credentials.new(ENV['AWS_ACCESS_KEY'], ENV['AWS_SECRET'])
16
+ )
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,65 @@
1
+
2
+
3
+ module Meroku
4
+ module CLI
5
+ class Session
6
+ include Meroku::CLI
7
+ include Meroku::Infrastructure
8
+
9
+ attr_accessor :ec2_client, :token
10
+
11
+ def initialize(*args)
12
+ case args.join(" ")
13
+ when "signup"
14
+ signup
15
+ when "create"
16
+ Meroku::CLI::User.load_secrets(self) || exit
17
+ create
18
+ when "keys:add"
19
+ Meroku::CLI::User.load_secrets(self) || exit
20
+ keys_add(self)
21
+ when "infrastructure spawn"
22
+ Meroku::CLI::AdminUser.load_secrets(self) || exit
23
+ spawn(self)
24
+ when "infrastructure despawn"
25
+ Meroku::CLI::AdminUser.load_secrets(self) || exit
26
+ despawn
27
+ else
28
+ puts "Unknown command #{args.join(" ")}\n\n"
29
+ puts help
30
+ end
31
+
32
+
33
+ #case args.join(" ")
34
+ # when "infrastructure spawn"
35
+ # load_secrets || exit
36
+ # node = Meroku::Infrastructure::Node.new.associate_address.install_packages.install_frontend_app
37
+ # puts "spawned #{node.instance.try(:instance_id)}"
38
+ # when "infrastructure despawn"
39
+ # load_secrets || exit
40
+ # Meroku::Infrastructure.despawn
41
+ # when "signup"
42
+ # signup
43
+ # when "keys:add"
44
+ # token_check || exit
45
+ # keys_add
46
+ # when "create"
47
+ # token_check || exit
48
+ # create
49
+ # else
50
+ # puts HELP
51
+ #end
52
+ end
53
+
54
+ def ec2_client
55
+ @ec2_client ||= ::Aws::EC2::Client.new(
56
+ region: 'us-east-1',
57
+ credentials: ::Aws::Credentials.new(ENV['AWS_ACCESS_KEY'], ENV['AWS_SECRET'])
58
+ )
59
+ @ec2_client
60
+ end
61
+
62
+ end
63
+ end
64
+ end
65
+
@@ -0,0 +1,14 @@
1
+ module Meroku
2
+ module CLI
3
+ module User
4
+ def self.load_secrets(obj)
5
+ if !File.exist? "#{Dir.home}/.meroku/.token"
6
+ puts "error: Have you logged in yet?"
7
+ return nil
8
+ end
9
+ obj.token = `cat ~/.meroku/.token`.chomp
10
+ end
11
+
12
+ end
13
+ end
14
+ end
@@ -3,15 +3,24 @@ require "meroku/infrastructure/node"
3
3
  module Meroku
4
4
  module Infrastructure
5
5
 
6
- extend Meroku::Aws
6
+ def spawn(session)
7
+ node = Meroku::Infrastructure::Node.new(session.ec2_client)
8
+ node.associate_address
9
+ node.install_packages
10
+ node.tweak_configuration
11
+ node.install_frontend_app
12
+ puts "spawned #{node.instance.try(:instance_id)}"
13
+ end
7
14
 
8
- def self.despawn
15
+ def despawn
9
16
  instances = ec2_client.describe_instances(filters:[{ name: "tag:Name", values: ['node'] }, { name: 'instance-state-name', values: ['running','pending'] }]).reservations.map { |xx| xx.instances.first.instance_id }
10
17
  puts "will despawn #{instances.inspect}"
11
18
  ec2_client.terminate_instances({
12
19
  instance_ids: instances
13
20
  }) if instances.size > 0
14
21
  end
22
+
23
+
15
24
  end
16
25
 
17
26
  end
@@ -2,11 +2,31 @@
2
2
  module Meroku
3
3
  module Infrastructure
4
4
  class Node
5
- include Meroku::Aws
6
- attr_accessor :instance, :tunnel
5
+ attr_accessor :instance, :tunnel, :ec2_client
7
6
 
8
- def initialize
9
- result = ec2_client.try(:run_instances,Meroku::Aws::EC2_PARAMS)
7
+ def initialize(ec2_client)
8
+ @ec2_client = ec2_client
9
+ result = ec2_client.try(
10
+ :run_instances,
11
+ {
12
+ image_id: 'ami-841f46ff', #was xenial 'ami-cd0f5cb6',
13
+ min_count: 1,
14
+ max_count: 1,
15
+ key_name: 'meroku.id_rsa',
16
+ instance_type: 't2.micro',
17
+ tag_specifications: [
18
+ {
19
+ resource_type: "instance",
20
+ tags: [
21
+ {
22
+ key: "Name",
23
+ value: "node",
24
+ },
25
+ ],
26
+ },
27
+ ]
28
+ }
29
+ )
10
30
  @instance = result.instances.first if result
11
31
  @tunnel = Meroku::Tunnel.new(
12
32
  ip: "34.239.241.218",
@@ -20,8 +40,7 @@ module Meroku
20
40
 
21
41
  def associate_address
22
42
  retries ||= 0
23
- return self if !ec2_client
24
- ec2_client.associate_address(
43
+ @ec2_client.associate_address(
25
44
  allocation_id: "eipalloc-139f7823",
26
45
  instance_id: @instance.try(:instance_id)
27
46
  )
@@ -43,6 +62,12 @@ module Meroku
43
62
  self
44
63
  end
45
64
 
65
+ def tweak_configuration
66
+ @tunnel.run 'sudo adduser --disabled-password --gecos "" git'
67
+ @tunnel.run 'sudo -u git mkdir /home/git/.ssh/'
68
+ @tunnel.run 'sudo -u git touch /home/git/.ssh/authorized_keys'
69
+ end
70
+
46
71
  def install_frontend_app
47
72
  @tunnel.run 'mkdir /home/ubuntu/.meroku'
48
73
  @tunnel.run "cd ~\; git clone https://github.com/oystersauce8/meroku\;"
@@ -1,3 +1,3 @@
1
1
  module Meroku
2
- VERSION = "2.0.3"
2
+ VERSION = "2.0.4"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: meroku
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.3
4
+ version: 2.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Weerasinghe
@@ -399,8 +399,10 @@ files:
399
399
  - frontend/tmp/.keep
400
400
  - frontend/vendor/.keep
401
401
  - lib/meroku.rb
402
- - lib/meroku/aws.rb
403
402
  - lib/meroku/cli.rb
403
+ - lib/meroku/cli/admin_user.rb
404
+ - lib/meroku/cli/session.rb
405
+ - lib/meroku/cli/user.rb
404
406
  - lib/meroku/infrastructure.rb
405
407
  - lib/meroku/infrastructure/node.rb
406
408
  - lib/meroku/tunnel.rb
@@ -1,32 +0,0 @@
1
- module Meroku
2
- module Aws
3
-
4
- EC2_PARAMS = {
5
- image_id: 'ami-841f46ff', #was xenial 'ami-cd0f5cb6',
6
- min_count: 1,
7
- max_count: 1,
8
- key_name: 'meroku.id_rsa',
9
- instance_type: 't2.micro',
10
- tag_specifications: [
11
- {
12
- resource_type: "instance",
13
- tags: [
14
- {
15
- key: "Name",
16
- value: "node",
17
- },
18
- ],
19
- },
20
- ]
21
- }
22
-
23
- def credentials
24
- ::Aws::Credentials.new(ENV['AWS_ACCESS_KEY'], ENV['AWS_SECRET'])
25
- end
26
-
27
- def ec2_client
28
- ::Aws::EC2::Client.new(region: 'us-east-1', credentials: credentials)
29
- end
30
-
31
- end
32
- end