Class: RightScaleSelfService::Cli::Template
- Defined in:
- lib/rightscale_selfservice/cli/template.rb
Instance Method Summary collapse
- #compile(filepath) ⇒ Object
- #execute(filepath) ⇒ Object
- #list ⇒ Object
- #preprocess(filepath) ⇒ Object
- #publish(filepath) ⇒ Object
- #upsert(filepath) ⇒ Object
Instance Method Details
#compile(filepath) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/rightscale_selfservice/cli/template.rb', line 42 def compile(filepath) source_filepath = File.(filepath, Dir.pwd) source_filename = File.basename(source_filepath) source_dir = File.dirname(source_filepath) result = RightScaleSelfService::Utilities::Template.preprocess(source_filepath) client = get_api_client() logger.info("Uploading #{source_filepath} to validate syntax") begin client.designer.template.compile(:source => result) logger.info("#{source_filepath} compiled successfully!") rescue RestClient::ExceptionWithResponse => e shell = Thor::Shell::Color.new = "Failed to compile template\n\n#{RightScaleSelfService::Api::Client.format_error(e)}" logger.error(shell.set_color , :red) end end |
#execute(filepath) ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/rightscale_selfservice/cli/template.rb', line 161 def execute(filepath) source_filepath = File.(filepath, Dir.pwd) source_filename = File.basename(source_filepath) source_dir = File.dirname(source_filepath) result = RightScaleSelfService::Utilities::Template.preprocess(source_filepath) client = get_api_client() params = {:source => result} if ["options_file"] = File.(["options_file"], Dir.pwd) = File.open(File.(), 'r') { |f| f.read } params["options"] = end begin exec_response = client.manager.execution.create(params) logger.info("Successfully started execution. Href: #{exec_response.headers[:location]}") rescue RestClient::ExceptionWithResponse => e shell = Thor::Shell::Color.new = "Failed to create execution from template\n\n#{RightScaleSelfService::Api::Client.format_error(e)}" logger.error(shell.set_color , :red) end end |
#list ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/rightscale_selfservice/cli/template.rb', line 141 def list client = get_api_client() begin list_response = client.designer.template.index() templates = JSON.parse(list_response.body) if ["property"] templates.each do |template| template.delete_if{|k,v| !(["property"].include?(k))} end end puts JSON.pretty_generate(templates) rescue RestClient::ExceptionWithResponse => e shell = Thor::Shell::Color.new = "Failed to list templates\n\n#{RightScaleSelfService::Api::Client.format_error(e)}" logger.error(shell.set_color , :red) end end |
#preprocess(filepath) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/rightscale_selfservice/cli/template.rb', line 28 def preprocess(filepath) source_filepath = File.(filepath, Dir.pwd) source_filename = File.basename(source_filepath) source_dir = File.dirname(source_filepath) dest_filepath = .has_key?('o') ? File.(['o'], Dir.pwd) : File.join(source_dir, "processed-#{source_filename}") logger.info("Preprocessing #{source_filepath} and writing result to #{dest_filepath}") result = RightScaleSelfService::Utilities::Template.preprocess(source_filepath) File.open(dest_filepath, 'w') {|f| f.write(result)} logger.info("Done! Find your file at #{dest_filepath}") end |
#publish(filepath) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/rightscale_selfservice/cli/template.rb', line 105 def publish(filepath) shell = Thor::Shell::Color.new template_href = upsert(filepath) template_id = template_href.split("/").last client = get_api_client() logger.info("Publishing template Href: #{template_href}") publish_params = {:id => template_id} begin response = client.designer.template.publish(publish_params) logger.info("Successfully published template.") rescue RestClient::ExceptionWithResponse => e if e.http_code == 409 && ["override"] logger.warn("Template id \"#{template_id}\" has already been published, but --override was set so we'll try to publish again with the overridden_application_href parameter.") begin app_response = client.catalog.application.index() applications = JSON.parse(app_response.body) matching_apps = applications.select{|a| a["template_info"]["href"] == template_href} if matching_apps == 0 logger.error(shell.set_color "Unable to find the published application for template id \"#{template_id}\"") else publish_params[:overridden_application_href] = matching_apps.first["href"] retry end rescue RestClient::ExceptionWithResponse => e = "Failed to get a list of existing published templates\n\n#{RightScaleSelfService::Api::Client.format_error(e)}" logger.error(shell.set_color , :red) end else = "Failed to publish template\n\n#{RightScaleSelfService::Api::Client.format_error(e)}" logger.error(shell.set_color , :red) end end end |
#upsert(filepath) ⇒ Object
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 |
# File 'lib/rightscale_selfservice/cli/template.rb', line 60 def upsert(filepath) template_href = "" source_filepath = File.(filepath, Dir.pwd) source_filename = File.basename(source_filepath) source_dir = File.dirname(source_filepath) template = RightScaleSelfService::Utilities::Template.preprocess(source_filepath) client = get_api_client() matches = template.match(/^name\s*"(?<name>.*)"/) tmp_file = matches["name"].gsub("/","-").gsub(" ","-") name = matches["name"] logger.info("Fetching a list of existing templates to see if \"#{name}\" exists...") templates = JSON.parse(client.designer.template.index.body) existing_templates = templates.select{|t| t["name"] == name } tmpfile = Tempfile.new([tmp_file,".cat.rb"]) begin tmpfile.write(template) tmpfile.rewind if existing_templates.length != 0 logger.info("A template named \"#{name}\" already exists, updating it...") template_id = existing_templates.first()["id"] request = client.designer.template.update({:id => template_id, :source => tmpfile}, true) response = request.execute template_href = client.get_relative_href(request.url) else logger.info("Creating template \"#{name}\"...") response = client.designer.template.create({:source => tmpfile}) template_href = response.headers[:location] end logger.info("Successfully upserted \"#{name}\". Href: #{template_href}") rescue RestClient::ExceptionWithResponse => e shell = Thor::Shell::Color.new = "Failed to update or create template\n\n#{RightScaleSelfService::Api::Client.format_error(e)}" logger.error(shell.set_color , :red) ensure tmpfile.close!() end template_href end |