Class: Jets::Remote::Runner

Inherits:
Base
  • Object
show all
Defined in:
lib/jets/remote/runner.rb

Instance Method Summary collapse

Methods inherited from Base

#initialize

Methods included from Util::Logging

#log

Methods included from AwsServices

#apigateway, #aws_options, #cfn, #codebuild, #dynamodb, #lambda_client, #logs, #s3, #s3_resource, #sns, #sqs, #ssm, #sts, #wafv2

Methods included from AwsServices::StackStatus

#output_value, #stack_exists?

Methods included from AwsServices::GlobalMemoist

included, #reset_cache!

Constructor Details

This class inherits a constructor from Jets::Remote::Base

Instance Method Details

#architectureObject

Get architecture of codebuild image being used



52
53
54
55
56
57
58
59
60
# File 'lib/jets/remote/runner.rb', line 52

def architecture
  project = codebuild.batch_get_projects(names: [project_name]).projects.first
  image = project.environment.image # IE: aws/codebuild/amazonlinux-aarch64-lambda-standard:ruby3.2
  if image.include?("aarch64") || image.include?("arm64")
    "arm64"
  else
    "x86_64"
  end
end

#code_zip_and_uploadObject



46
47
48
49
# File 'lib/jets/remote/runner.rb', line 46

def code_zip_and_upload
  code = Jets::Code.new(dummy: @options[:dummy])
  @app_src = code.zip_and_upload # s3_location
end

#environment_variables_overrideObject



81
82
83
84
85
86
87
# File 'lib/jets/remote/runner.rb', line 81

def environment_variables_override
  Jets::Cfn::Resource::Codebuild::Project::Env.new.pass_vars(
    JETS_APP_SRC: @app_src,
    JETS_GO: @jets_go,
    JETS_SIG: @@sig
  )
end

#project_nameObject



62
63
64
65
66
67
# File 'lib/jets/remote/runner.rb', line 62

def project_name
  stack_name = Jets::Names.parent_stack_name
  stack = cfn.describe_stacks(stack_name: stack_name).stacks.first
  logical_id = use_lambda_compute_type? ? "CodebuildLambda" : "Codebuild"
  stack.outputs.find { |o| o.output_key == logical_id }.output_value
end

#runObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/jets/remote/runner.rb', line 4

def run
  code_zip_and_upload

  command = @options[:command] || "deploy"
  command_args = if @options[:version] # jets rollback 8
    @options[:version]
  elsif @options[:templates] # jets build --templates or jets deploy --templates
    "--templates"
  end

  sig = Jets::Api::Sig.create(command: command, command_args: command_args, architecture: architecture)
  @@sig = sig[:token]
  @jets_go = sig[:jets_go]

  params = {
    buildspec_override: sig[:buildspec],
    environment_variables_override: environment_variables_override,
    project_name: project_name
  }
  fleet_override = Jets.bootstrap.config.codebuild.project.fleet_override
  if fleet_override
    params[:fleet_override] = {fleet_arn: fleet_override}
  end

  resp = codebuild.start_build(params)
  Jets::Api::Sig.update(sig[:id], build_id: resp.build.id)

  logger.info "Started remote run for #{command}"
  Jets::CLI::Tip.show(:remote_run, disable_howto: false)
  logger.info "Console Log Url:"
  logger.info codebuild_log_url(resp.build.id)

  tail_logs(resp.build.id)

  Download.new(command).download_built

  resp = codebuild.batch_get_builds(ids: [resp.build.id])
  success = resp.builds.first.build_status == "SUCCEEDED"
  exit 1 unless success
  success
end

#tail_logs(build_id) ⇒ Object



89
90
91
# File 'lib/jets/remote/runner.rb', line 89

def tail_logs(build_id)
  Tailer.new(@options, build_id).run
end

#use_lambda_compute_type?Boolean

Returns:

  • (Boolean)


70
71
72
73
74
75
76
77
78
79
# File 'lib/jets/remote/runner.rb', line 70

def use_lambda_compute_type?
  return true if Jets.bootstrap.config.infra
  return false unless Jets.bootstrap.config.codebuild.lambda.enable

  command = ARGV.first
  command == "ci" ||
    command == "waf" ||
    command == "dockerfile" ||
    ARGV.include?("--templates")
end