Class: DeusEx::Machina
- Inherits:
-
Object
- Object
- DeusEx::Machina
- Defined in:
- lib/deus_ex/machina.rb
Constant Summary collapse
- NAME =
'deus_ex'
- DEPLOY_PROJECT =
'deus_ex_project'
- GIT_REMOTE_NAME =
'deus-ex'
Class Method Summary collapse
Instance Method Summary collapse
- #clean_up ⇒ Object
- #compute ⇒ Object
- #config_read ⇒ Object
- #create_floating_ip ⇒ Object
- #git_remote ⇒ Object
- #log(message, level = :info, logger = Logger.new($stdout)) ⇒ Object
- #network ⇒ Object
- #servers ⇒ Object
- #setup_git_remote ⇒ Object
- #setup_repository ⇒ Object
- #setup_server ⇒ Object
- #status ⇒ Object
- #test_ssh ⇒ Object
- #warn(message) ⇒ Object
Class Method Details
.cleanup ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/deus_ex/machina.rb', line 26 def self.cleanup machina = new machina.config_read machina.clean_up rescue Exception => e if e.is_a?(SystemExit) machina.log "Exiting" else machina.log "error: #{e.inspect}" machina.clean_up raise e end end |
.setup ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/deus_ex/machina.rb', line 10 def self.setup machina = new machina.config_read machina.setup_server machina.setup_repository machina.setup_git_remote rescue Exception => e if e.is_a?(SystemExit) machina.log "Exiting" else machina.log "error: #{e.inspect}" machina.clean_up raise e end end |
.status ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/deus_ex/machina.rb', line 40 def self.status machina = new machina.config_read machina.status rescue Exception => e if e.is_a?(SystemExit) machina.log "Exiting" else machina.log "error: #{e.inspect}" machina.clean_up raise e end end |
Instance Method Details
#clean_up ⇒ Object
142 143 144 145 |
# File 'lib/deus_ex/machina.rb', line 142 def clean_up destroyed = servers.map(&:destroy).count log "#{destroyed} server#{'s' if destroyed != 1} destroyed" end |
#compute ⇒ Object
54 55 56 57 58 |
# File 'lib/deus_ex/machina.rb', line 54 def compute connection = ::Fog::Compute.new(@config[:authentication].dup) log "connection established" connection end |
#config_read ⇒ Object
66 67 68 |
# File 'lib/deus_ex/machina.rb', line 66 def config_read @config = YAML.load_file('.machina.yml') if @config.nil? end |
#create_floating_ip ⇒ Object
86 87 88 89 90 91 92 93 |
# File 'lib/deus_ex/machina.rb', line 86 def create_floating_ip floater = network.floating_ips.create(@config[:floating_ip_create]) floating_id = floater.id @ip = floater.floating_ip_address @port = 22 port = network.ports(:filters => { :device_id => @server.id }).first network.associate_floating_ip(floating_id, port.id) end |
#git_remote ⇒ Object
155 156 157 |
# File 'lib/deus_ex/machina.rb', line 155 def git_remote "#{@config[:username]}@#{@server.public_ip_address}:#{DEPLOY_PROJECT}.git" end |
#log(message, level = :info, logger = Logger.new($stdout)) ⇒ Object
159 160 161 162 163 164 165 |
# File 'lib/deus_ex/machina.rb', line 159 def log(, level = :info, logger = Logger.new($stdout)) logger.formatter = proc do |severity, datetime, progname, msg| "[DEUS EX] #{msg}\n" end logger.send(level, ) end |
#network ⇒ Object
60 61 62 63 64 |
# File 'lib/deus_ex/machina.rb', line 60 def network authentication = @config[:authentication].dup authentication.delete(:version) ::Fog::Network.new(authentication) end |
#servers ⇒ Object
151 152 153 |
# File 'lib/deus_ex/machina.rb', line 151 def servers @server ? [@server] : compute.servers.select { |s| s.name == NAME } end |
#setup_git_remote ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/deus_ex/machina.rb', line 124 def setup_git_remote if system('git rev-parse') log "adding local git remote" system "git remote add #{GIT_REMOTE_NAME} #{git_remote}" log "pushing to remote" system "ssh-agent bash -c 'ssh-add #{@config[:private_key_path]}; git push #{GIT_REMOTE_NAME} master'" log "removing local git remote" system "git remote rm #{GIT_REMOTE_NAME}" log "" log "you can now deploy from #{git_remote}" else warn "not in a git repo" end end |
#setup_repository ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/deus_ex/machina.rb', line 108 def setup_repository log "initializing git repo" @server.private_key_path = @config[:private_key_path] = { :port => @port, :key_data => [File.read(@config[:private_key_path])], :auth_methods => ["publickey"] } log "." until test_ssh Fog::SSH.new(@ip, @config[:username], ).run([ "mkdir #{DEPLOY_PROJECT}.git", "cd #{DEPLOY_PROJECT}.git && git init --bare" ]) log "git repo initialized" end |
#setup_server ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/deus_ex/machina.rb', line 70 def setup_server log "creating server (this may take a couple of minutes)" server_config = @config[:server_create].dup server_config[:name] = NAME @server = compute.servers.create(server_config) log "server created" @server.wait_for { print '.'; ready? } log "server ready" if @config[:floating_ip_create].nil? @ip = @server.public_ip_address else create_floating_ip end log "server ip " + @ip end |
#status ⇒ Object
147 148 149 |
# File 'lib/deus_ex/machina.rb', line 147 def status log "#{servers.count} server#{'s' if servers.count != 1} found" end |
#test_ssh ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/deus_ex/machina.rb', line 95 def test_ssh socket = TCPSocket.new(@ip, @port) IO.select([socket], nil, nil, 5) rescue SocketError, Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Errno::ENETUNREACH, IOError sleep 2 false rescue Errno::EPERM, Errno::ETIMEDOUT false ensure socket && socket.close end |
#warn(message) ⇒ Object
167 168 169 |
# File 'lib/deus_ex/machina.rb', line 167 def warn() log , :warn end |