Class: Singularity::Runner
- Inherits:
-
Object
- Object
- Singularity::Runner
- Defined in:
- lib/singularity.rb
Instance Method Summary collapse
-
#initialize(script) ⇒ Runner
constructor
A new instance of Runner.
- #is_paused ⇒ Object
- #runner ⇒ Object
Constructor Details
#initialize(script) ⇒ Runner
Returns a new instance of Runner.
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 118 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 145 |
# File 'lib/singularity.rb', line 81 def initialize(script) ######################################################### # TODO # check to see that .mescal.json and mesos-deploy.yml exist ######################################################### @script = script # read .mescal.json for ssh command, image, release number, cpus, mem @configData = JSON.parse(ERB.new(open(File.join(Dir.pwd, ".mescal.json")).read).result(Request.new.get_binding)) @sshCmd = @configData['sshCmd'] @image = @configData['image'].split(':')[0] @release = @configData['image'].split(':')[1] # read mesos-deploy.yml for singularity url @mesosDeployConfig = YAML.load_file(File.join(Dir.pwd, "mesos-deploy.yml")) @uri = @mesosDeployConfig['singularity_url'] # create request/deploy json data @data = { 'command' => "/sbin/my_init", 'resources' => { 'memoryMb' => @configData['mem'], 'cpus' => @configData['cpus'], 'numPorts' => 1 }, 'env' => { 'APPLICATION_ENV' => "production" }, 'requestType' => "RUN_ONCE", 'containerInfo' => { 'type' => "DOCKER", 'docker' => { 'image' => @configData['image'], 'network' => "BRIDGE", 'portMappings' => [{ 'containerPortType': "LITERAL", 'containerPort': 22, 'hostPortType': "FROM_OFFER", 'hostPort': 0 }] } } } # either we typed 'singularity ssh' if @script == "ssh" @data['id'] = Dir.pwd.split('/').last + "_SSH" @data['command'] = "#{@sshCmd}" # or we passed a script/commands to 'singularity run' else # if we passed "runx", then skip use of /sbin/my_init if @script[0] == "runx" @data['arguments'] = [] # don't use "--" as first argument @data['command'] = @script[1] #remove "runx" from commands @script.shift @data['id'] = @script.join("-").tr('@/\*?% []#$', '_') @data['id'][0] = '' @script.shift # else join /sbin/my_init with your commands else @data['arguments'] = ["--"] @data['id'] = @script.join("-").tr('@/\*?% []#$', '_') @data['id'][0] = '' end @script.each { |i| @data['arguments'].push i } end end |
Instance Method Details
#is_paused ⇒ Object
147 148 149 150 151 152 153 154 155 |
# File 'lib/singularity.rb', line 147 def is_paused begin resp = RestClient.get "#{@uri}/api/requests/request/#{@data['id']}" JSON.parse(resp)['state'] == 'PAUSED' rescue print " Deploying request...".light_green false end end |
#runner ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/singularity.rb', line 157 def runner begin if is_paused() puts " PAUSED, SKIPPING.".yellow return else # create or update the request RestClient.post "#{@uri}/api/requests", @data.to_json, :content_type => :json end # deploy the request @data['requestId'] = @data['id'] @data['id'] = "#{@release}.#{Time.now.to_i}" @deploy = { 'deploy' => @data, 'user' => `whoami`.chomp, 'unpauseOnSuccessfulDeploy' => false } RestClient.post "#{@uri}/api/deploys", @deploy.to_json, :content_type => :json puts " Deploy succeeded.".green # get active tasks until ours shows up so we can get IP/PORT begin @thisTask = '' @tasks = JSON.parse(RestClient.get "#{@uri}/api/tasks/active", :content_type => :json) @tasks.each do |entry| if entry['taskRequest']['request']['id'] == @data['requestId'] @thisTask = entry end end end until @thisTask != '' @ip = @thisTask['offer']['url']['address']['ip'] @port = @thisTask['mesosTask']['container']['docker']['portMappings'][0]['hostPort'] # SSH into the machine if @script == "ssh" # uses "begin end until" because "system" will keep returning "false" unless the command exits with success # this makes sure that the docker image has completely started and the SSH command succeeds where = Dir.pwd.split('/').last puts " Opening a shell to #{where}, please wait a moment...".light_blue begin end until system "ssh -o LogLevel=quiet -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no root@#{@ip} -p #{@port}" else puts " Deployed and running #{@data['command']} #{@data['arguments']}".light_green print " STDOUT".light_cyan print " and" print " STDERR".light_magenta puts ":" # output STDOUT / STDERR to shell @stdoutOffset = 0 @stderrOffset = 0 @lastOutLine = '' @lastErrLine = '' begin # get most recent task state @taskState = JSON.parse(RestClient.get "#{@uri}/api/history/task/#{@thisTask['taskId']['id']}") @taskState["taskUpdates"].each do |update| @taskState = update['taskState'] end # need to wait for "task_running" before we can ask for STDOUT/STDERR if @taskState == "TASK_RUNNING" @stdout = JSON.parse(RestClient.get "#{@uri}/api/sandbox/#{@thisTask['taskId']['id']}/read", {params: {path: "stdout", length: 30000, offset: @stdoutOffset}})['data'] @stdoutOffset += @stdout.length @stdout = @stdout.split("\n") if @stdout.any? if @lastOutLine.include? @stdout[0] @stdout.shift end if !@stdout.empty? if @stdout[0].length > 0 @stdout.each do |i| puts i.light_cyan end end end @lastOutLine = @stdout.last end @stderr = JSON.parse(RestClient.get "#{@uri}/api/sandbox/#{@thisTask['taskId']['id']}/read", {params: {path: "stderr", length: 30000, offset: @stderrOffset}})['data'] @stderrOffset += @stderr.length @stderr = @stderr.split("\n") if @stderr.any? if @lastErrLine.include? @stderr[0] @stderr.shift end if !@stderr.empty? if @stderr[0].length > 0 @stderr.each do |i| puts i.light_magenta end end end @lastErrLine = @stderr.last end end end until @taskState == "TASK_FINISHED" end # finally, delete the request (which also deletes the corresponding task) RestClient.delete "#{@uri}/api/requests/request/#{@data['requestId']}" rescue Exception => e puts " #{e.response}".red end end |