Module: Viaduct::Toolkit::Helpers
- Defined in:
- lib/viaduct/toolkit/helpers.rb
Instance Method Summary collapse
- #boolean(bool) ⇒ Object
- #chech_ssh_key_presence ⇒ Object
- #details(&block) ⇒ Object
- #ensure_logged_in! ⇒ Object
- #error(message) ⇒ Object
- #exec_console_command(console, command) ⇒ Object
- #field(key, value) ⇒ Object
- #find_application ⇒ Object
- #find_database(app, database_name = nil) ⇒ Object
- #get_application_console_port_forward(app) ⇒ Object
- #heading(title) ⇒ Object
- #length_of_time(seconds) ⇒ Object
- #run(*commands) ⇒ Object
- #table(headings, rows) ⇒ Object
- #time(time) ⇒ Object
- #validation_errors(errors) ⇒ Object
Instance Method Details
#boolean(bool) ⇒ Object
27 28 29 |
# File 'lib/viaduct/toolkit/helpers.rb', line 27 def boolean(bool) bool ? "\u2713".green : "-".red end |
#chech_ssh_key_presence ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/viaduct/toolkit/helpers.rb', line 132 def chech_ssh_key_presence response = Viaduct::Toolkit.api.ssh_keys.all if response.success? if response.data.empty? puts "You haven't uploaded any SSH keys to your Viaduct user account.".red puts "You cannot use SSH console access without them.".red puts puts "Upload your key using the command below".blue puts puts " $ vdt ssh_key:add".blue puts exit(1) else stdout, stderr, status = run("ssh-add -l") if status == 0 remote_fingerprints = response.data.map { |d| d['fingerprint'] } local_fingerprints = stdout.split(/\n/).map { |l| l.split(/\s+/)[1] } unless remote_fingerprints.any? { |f| local_fingerprints.include?(f) } puts "Warning: it doesn't seem as though your SSH key has been uploaded".yellow puts "to your Viaduct account. This session may not succeed. If it doesn't".yellow puts "ensure that you have uploaded to your SSH key to your Viaduct account.".yellow end end end else error "Couldn't verify remote SSH keys. Please try again." end end |
#details(&block) ⇒ Object
127 128 129 130 |
# File 'lib/viaduct/toolkit/helpers.rb', line 127 def details(&block) block.call puts "+" + ("-" * 78) + "+" end |
#ensure_logged_in! ⇒ Object
6 7 8 9 10 11 12 13 14 15 |
# File 'lib/viaduct/toolkit/helpers.rb', line 6 def ensure_logged_in! if Viaduct::Toolkit.env_config['token'].nil? && Viaduct::Toolkit.env_config['secret'].nil? puts "You need to login before using this toolkit. Use the command below".yellow puts "to login to your Viaduct account.".yellow puts puts " $ vdt login" puts exit 1 end end |
#error(message) ⇒ Object
44 45 46 |
# File 'lib/viaduct/toolkit/helpers.rb', line 44 def error() raise Viaduct::Toolkit::Error, end |
#exec_console_command(console, command) ⇒ Object
175 176 177 178 179 180 181 182 183 184 |
# File 'lib/viaduct/toolkit/helpers.rb', line 175 def exec_console_command(console, command) unless console['enabled'] response = Viaduct::Toolkit.api.port_forwards.save(:id => console['id'], :enabled => 1, :auto_disable_at => '5 minutes from now') unless response.success? error "We couldn't enable console access at this time. Please try later." end end puts "Connecting...".magenta exec(command) end |
#field(key, value) ⇒ Object
120 121 122 123 124 125 |
# File 'lib/viaduct/toolkit/helpers.rb', line 120 def field(key, value) key = key[0,16].ljust(16, ' ') value = value.to_s[0,58].ljust(58) puts "| #{key.blue}| #{value} |" end |
#find_application ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/viaduct/toolkit/helpers.rb', line 53 def find_application if $app.is_a?(String) && $app.length > 0 app = Viaduct::Toolkit.api.applications.info(:application => $app) if app.success? return app.data else puts "Couldn't find application with subdomain matching '#{$app}'".red exit(1) end else # Look up from repo out, err, status = run("git", "remote", "-v") if status == 0 potential_repositories = out.split("\n").map { |l| l.split(/\s+/)[1] }.uniq app = Viaduct::Toolkit.api.applications.all(:filter => {:repo => potential_repositories}) if app.success? if app.data.empty? puts "No Viaduct applications found for any of the following repositories:".red potential_repositories.each do |repo| puts " * #{repo}".red end exit(1) elsif app.data.size == 1 $app = app.data.first['subdomain'] return find_application else puts "Multiple applications found matching your repository. Choose an application...".yellow choice = choose('', *app.data.map { |a| "#{a['subdomain']}: #{a['name']}"}) choice = choice.split(":", 2).first $app = choice return find_application() end end end end puts "Couldn't determine a Viaduct application from command.".red exit 1 end |
#find_database(app, database_name = nil) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/viaduct/toolkit/helpers.rb', line 93 def find_database(app, database_name = nil) # Is there a DB? if database_name database_fruit = database_name elsif app['main_database'] database_fruit = app['main_database']['fruit'] else puts "There is no database specified and you don't have a main database for".red puts "this application. Try again using the --database option.".red exit 1 end database = Viaduct::Toolkit.api.applications.database(:application => app['subdomain'], :database => database_fruit) if database.success? database.data else puts "No database found matching '#{database_fruit}'".red exit 1 end end |
#get_application_console_port_forward(app) ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/viaduct/toolkit/helpers.rb', line 162 def get_application_console_port_forward(app) response = Viaduct::Toolkit.api.port_forwards.all(:application => app['subdomain']) if response.success? if console = response.data.select { |c| c['mode'] == 'console'}.first console else error "Console access is not supported by this application. Please contact support." end else error "Couldn't get port forward information from API for application." end end |
#heading(title) ⇒ Object
114 115 116 117 118 |
# File 'lib/viaduct/toolkit/helpers.rb', line 114 def heading(title) puts "+" + ("-" * 78) + "+" puts "| #{title.ljust(76).yellow} |" puts "+" + ("-" * 78) + "+" end |
#length_of_time(seconds) ⇒ Object
17 18 19 |
# File 'lib/viaduct/toolkit/helpers.rb', line 17 def length_of_time(seconds) "#{seconds} seconds" end |
#run(*commands) ⇒ Object
48 49 50 51 |
# File 'lib/viaduct/toolkit/helpers.rb', line 48 def run(*commands) stdin, stdout, stderr, w = Open3.popen3(*commands) [stdout.gets(nil), stderr.gets(nil), w.value] end |
#table(headings, rows) ⇒ Object
31 32 33 34 |
# File 'lib/viaduct/toolkit/helpers.rb', line 31 def table(headings, rows) require 'terminal-table' puts Terminal::Table.new :rows => rows, :headings => headings.map(&:blue) end |
#time(time) ⇒ Object
21 22 23 24 25 |
# File 'lib/viaduct/toolkit/helpers.rb', line 21 def time(time) require 'time' time = Time.parse(time) rescue nil time ? time.strftime("%d %B %Y at %H:%M:%S UTC") : '' end |
#validation_errors(errors) ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/viaduct/toolkit/helpers.rb', line 36 def validation_errors(errors) errors.each do |field, | .each do || puts " * #{field} #{message}".red end end end |