Class: VaeLocal
- Inherits:
-
Object
- Object
- VaeLocal
- Defined in:
- lib/vae_local.rb
Constant Summary collapse
- BANNER =
"Vae local preview server, version #{VER}"
Class Method Summary collapse
- .auth_key_path ⇒ Object
- .fetch_from_vaeplatform(subdomain, req) ⇒ Object
- .port_open?(port) ⇒ Boolean
- .read_auth_keys ⇒ Object
- .run_trapping_exceptions ⇒ Object
- .vaeplatform_host(subdomain) ⇒ Object
- .vaeplatform_url(subdomain) ⇒ Object
- .write_auth_key(site, key) ⇒ Object
Instance Method Summary collapse
- #run ⇒ Object
- #show_job_status(res, site) ⇒ Object
- #stagerelease(action, site, auth_key, branch, built_path = nil) ⇒ Object
Class Method Details
.auth_key_path ⇒ Object
45 46 47 |
# File 'lib/vae_local.rb', line 45 def self.auth_key_path File.join(Dir.home, ".vae_local_credentials.yml") end |
.fetch_from_vaeplatform(subdomain, req) ⇒ Object
4 5 6 7 8 9 10 11 12 13 |
# File 'lib/vae_local.rb', line 4 def self.fetch_from_vaeplatform(subdomain, req) local = ENV['VAEPLATFORM_LOCAL'] http = Net::HTTP.new(vaeplatform_host(subdomain), (local ? 80 : 443)) http.use_ssl = true unless local http.verify_mode = OpenSSL::SSL::VERIFY_NONE http.start { |http| http.read_timeout = 120 http.request(req) } end |
.port_open?(port) ⇒ Boolean
15 16 17 |
# File 'lib/vae_local.rb', line 15 def self.port_open?(port) !system("lsof -i:#{port}", out: '/dev/null') end |
.read_auth_keys ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/vae_local.rb', line 27 def self.read_auth_keys if File.exists?(auth_key_path) data = YAML::load_file(auth_key_path) || {} else data = {} end if !data['site_keys'].is_a?(Hash) data['site_keys'] = {} end data end |
.run_trapping_exceptions ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/vae_local.rb', line 175 def self.run_trapping_exceptions begin new.run rescue VaeError => e cmd = $0 cmd = "vae" if cmd =~ /\.\.\/vae_local/ puts "** Error:" puts " " + e.to_s puts "Type #{cmd} --help for help." end end |
.vaeplatform_host(subdomain) ⇒ Object
19 20 21 |
# File 'lib/vae_local.rb', line 19 def self.vaeplatform_host(subdomain) "#{subdomain.split(".").first}." + (ENV['VAEPLATFORM_LOCAL'] ? "vaeplatform.test" : "vaeplatform.com") end |
.vaeplatform_url(subdomain) ⇒ Object
23 24 25 |
# File 'lib/vae_local.rb', line 23 def self.vaeplatform_url(subdomain) "http#{ENV['VAEPLATFORM_LOCAL'] ? '' : 's'}://#{vaeplatform_host(subdomain)}" end |
.write_auth_key(site, key) ⇒ Object
39 40 41 42 43 |
# File 'lib/vae_local.rb', line 39 def self.write_auth_key(site, key) data = read_auth_keys data['site_keys'][site] = key File.open(auth_key_path, 'w') { |f| f.write data.to_yaml } end |
Instance Method Details
#run ⇒ Object
49 50 51 52 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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/vae_local.rb', line 49 def run = { port: 9999, server: ProxyServer, log_level: "warning" } loop { break if VaeLocal.port_open?([:port]) [:port] = [:port] - 1 } ARGV. do |opts| opts. = BANNER + "\n\nUsage: vae [options]\n starts a local development server\n vae [options] deploy\n deploys the source in Git repository or provided URL to the Vae servers\n\n If you are using the Vae Production environment features:\n vae [options] stage\n deploys the source in Git repository or provided URL to the staging environment\n vae [options] stagerelease\n deploys the source in Git repository or provided URL to the staging environment\n and releases it to the production environment\n vae [options] release\n releases the current staging environment to the production environment\n vae [options] rollback\n rolls back the production environment to a previous release\n\nAvailable Options:" opts.on("-u","--username <username>","Your Vae username") { |o| [:username] = o } opts.on("-P","--password <password>","Your Vae password") { |o| [:password] = o } opts.on("-p","--port <port number>","Start server on this port") { |o| [:port] = o.to_i; raise VaeError "Port #{o.to_i} is already in use." unless VaeLocal.port_open?(o.to_i) } opts.on("-r","--root <path to site root>","Path to the root of the local copy of your Vae site.") { |o| [:site_root] = o } opts.on("-s","--site <subdomain>","Vae subdomain for this site") { |o| [:site] = o } opts.on("-f","--full-stack [php|hhvm]","Run in Full Stack Mode. Optionally provide 'php' or 'hhvm' to specify your preferred PHP runtime") { |o| [:server] = FullStack; [:php_runtime] = o } opts.on("-U","--url <url>","If running stage or stagerelease, provide the URL of a ZIP file to deploy here") { |o| [:branch] = o } opts.on("-b","--branch <branch>","If running stage or stagerelease, override the Git branch to deploy here") { |o| [:branch] = o } opts.on("-B","--built_path <path>","If running stage or stagerelease, and you want to provide the built files to deploy from a local path, provide that path here") { |o| [:built_path] = o } opts.on("-d","--data-path <path>","Where to Store Content and Image Data When In Full Stack Mode") { |o| [:data_path] = o } opts.on("-l","--log-level <level>","Vaedb Log Level (for Full Stack Mode)") { |o| [:log_level] = o } opts.on_tail("-h","--help", "Show this help message") { puts opts; exit } opts.parse! end [:site_root] = Dir.pwd if [:site_root].nil? and (File.exists?("#{Dir.pwd}/__vae.yml") or File.exists?("#{Dir.pwd}/__verb.yml")) if [:site_root] [ "verb", "vae" ].each do |name| if File.exists?("#{[:site_root]}/__#{name}.yml") site_conf_file = File.read("#{[:site_root]}/__#{name}.yml") site_conf = YAML.load(site_conf_file) [:site] ||= site_conf[name]["site"] if site_conf[name] and site_conf[name]["site"] end end end raise VaeError, "We could not determine the Vae subdomain for this site. Please specify it manually by using the --site option or create a __vae.yml file within the site root." if [:site].nil? keys = VaeLocal.read_auth_keys if key = keys['site_keys'][[:site]] [:auth_key] = key end unless [:auth_key] raise VaeError, "We could not determine the Vae username that you use. Please specify it manually by using the --username option." if [:username].nil? if [:password].nil? [:password] = ask("Please enter the Vae password for username #{[:username]}:") { |q| q.echo = false } end end if [ "deploy", "release", "rollback", "stage", "stagerelease" ].include?(ARGV.last) stageaction = true else raise VaeError, "You did not specify the path to the root of the local copy of your Vae site. Please specify it manually by using the --root option or cd to the site root (and make sure it contains a __vae.yml file)." unless [:site_root] raise VaeError, "You specified an invalid path to the local copy of your Vae site." unless File.exists?([:site_root]) end site = Site.new(subdomain: [:site], root: [:site_root], username: [:username], password: [:password], auth_key: [:auth_key]) if stageaction stagerelease(ARGV.last, [:site], site.auth_key, [:branch], [:built_path]) exit end Dir.chdir File.dirname(__FILE__) puts BANNER [:server].new(site, ).run puts "Thanks for using Vae!" end |
#show_job_status(res, site) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/vae_local.rb', line 119 def show_job_status(res, site) data = JSON.parse(res.body) if data['error'] raise VaeError, data['error'] else puts "Request started, waiting for completion..." loop do sleep 5 req = Net::HTTP::Get.new("/api/local/v1/job_status/#{data['job']}") res = VaeLocal.fetch_from_vaeplatform(site, req) status = JSON.parse(res.body) if status['status'] == "completed" if status['error'] and status['error'].size > 0 raise VaeError, "Got the following error from Vae Platform: #{status['error']}" else puts status['notice'] end return elsif status['status'] != "working" raise VaeError, "Got the following error from Vae Platform: #{status['error']}" end end end rescue JSON::ParserError raise VaeError, "An unknown error occurred requesting this operation from Vae Platform. Please email support for help." end |
#stagerelease(action, site, auth_key, branch, built_path = nil) ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/vae_local.rb', line 146 def stagerelease(action, site, auth_key, branch, built_path = nil) if action == "deploy" action = "stage" elsif action == "stagerelease" stagerelease("stage", site, auth_key, branch, built_path) stagerelease("release", site, auth_key, branch) return end path = "/api/local/v1/#{action}" if built_path zip_path = "/tmp/vae_local_zip.zip" Dir.chdir(built_path) `zip -r "#{zip_path}" *` res = RestClient.post(VaeLocal.vaeplatform_url(site) + path, file: File.open(zip_path, 'rb'), auth_key: auth_key, vae_local: "1") `rm #{zip_path}` else req = Net::HTTP::Post.new(path) req.body = "auth_key=#{CGI.escape(auth_key)}&branch=#{CGI.escape(branch || "")}&vae_local=1" res = VaeLocal.fetch_from_vaeplatform(site, req) end if res.is_a?(Net::HTTPFound) raise VaeError, "Invalid username/password or insufficient permissions." else show_job_status(res, site) end rescue RestClient::Found => e raise VaeError, "Invalid username/password or insufficient permissions." end |