Class: Shipyrd::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/shipyrd/client.rb

Defined Under Namespace

Classes: DestinationBlocked

Constant Summary collapse

ENV_VARS =
%w[
  KAMAL_COMMAND
  KAMAL_DESTINATION
  KAMAL_HOSTS
  KAMAL_PERFORMER
  KAMAL_RECORDED_AT
  KAMAL_ROLE
  KAMAL_RUNTIME
  KAMAL_SERVICE_VERSION
  KAMAL_SUBCOMMAND
  KAMAL_VERSION
  SHIPYRD_API_KEY
  SHIPYRD_COMMIT_MESSAGE
  SHIPYRD_HOST
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host: ENV["SHIPYRD_HOST"], api_key: ENV["SHIPYRD_API_KEY"], **options) ⇒ Client

Returns a new instance of Client.



22
23
24
25
26
27
# File 'lib/shipyrd/client.rb', line 22

def initialize(host: ENV["SHIPYRD_HOST"], api_key: ENV["SHIPYRD_API_KEY"], **options)
  @host = parse_host(host)
  @api_key = api_key
  @logger = options[:logger] || Shipyrd::Logger.new
  @valid_configuration = validate_configuration
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



2
3
4
# File 'lib/shipyrd/client.rb', line 2

def api_key
  @api_key
end

#hostObject (readonly)

Returns the value of attribute host.



2
3
4
# File 'lib/shipyrd/client.rb', line 2

def host
  @host
end

#loggerObject (readonly)

Returns the value of attribute logger.



2
3
4
# File 'lib/shipyrd/client.rb', line 2

def logger
  @logger
end

#valid_configurationObject (readonly)

Returns the value of attribute valid_configuration.



2
3
4
# File 'lib/shipyrd/client.rb', line 2

def valid_configuration
  @valid_configuration
end

Instance Method Details

#commit_messageObject



98
99
100
101
102
103
104
105
106
# File 'lib/shipyrd/client.rb', line 98

def commit_message
  message = `git show -s --format=%s`.chomp

  if message.length >= 90
    "#{message[0..90]}..."
  else
    message
  end
end

#envObject



84
85
86
# File 'lib/shipyrd/client.rb', line 84

def env
  ENV.slice(*ENV_VARS)
end

#github_usernameObject



92
93
94
95
96
# File 'lib/shipyrd/client.rb', line 92

def github_username
  `gh config get -h github.com username`.chomp
rescue
  "" # gh config get returns an empty string when not set
end

#parse_host(host) ⇒ Object



124
125
126
127
128
129
# File 'lib/shipyrd/client.rb', line 124

def parse_host(host)
  return "https://hooks.shipyrd.io" if host.nil?
  return host if host.start_with?("https")

  "https://#{host}"
end

#performerObject



88
89
90
# File 'lib/shipyrd/client.rb', line 88

def performer
  github_username.empty? ? ENV["KAMAL_PERFORMER"] : "https://github.com/#{github_username}"
end

#trigger(event) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
# File 'lib/shipyrd/client.rb', line 29

def trigger(event)
  return false unless valid_configuration

  uri = URI("#{host}/deploys.json")
  headers = {
    "Content-Type": "application/json",
    "Authorization": "Bearer #{api_key}"
  }

  details = {
    deploy: {
      status: event,
      recorded_at: ENV["KAMAL_RECORDED_AT"],
      performer: performer,
      commit_message: ENV["SHIPYRD_COMMIT_MESSAGE"] || commit_message,
      version: ENV["KAMAL_VERSION"],
      service_version: ENV["KAMAL_SERVICE_VERSION"],
      hosts: ENV["KAMAL_HOSTS"],
      command: ENV["KAMAL_COMMAND"],
      subcommand: ENV["KAMAL_SUBCOMMAND"],
      role: ENV["KAMAL_ROLE"],
      destination: ENV["KAMAL_DESTINATION"],
      runtime: ENV["KAMAL_RUNTIME"]
    }
  }

  http = Net::HTTP.new(uri.host, uri.port)
  http.read_timeout = 5
  http.write_timeout = 5
  http.open_timeout = 5
  http.use_ssl = true
  request = Net::HTTP::Post.new(uri.request_uri, headers)
  request.body = details.to_json

  response = http.request(request)

  if response.is_a?(Net::HTTPSuccess)
    logger.info "#{event} triggered successfully for #{details[:deploy][:service_version]}"
  elsif response.is_a?(Net::HTTPUnprocessableEntity)
    json_response = JSON.parse(response.body)

    if (lock = json_response.dig("errors", "lock"))
      raise DestinationBlocked, lock
    else
      logger.info "#{event} trigger failed with errors => #{json_response["errors"]}"
    end
  else
    logger.info "#{event} trigger failed with #{response.code}(#{response.message})"
  end
rescue DestinationBlocked
  raise
rescue => e
  logger.info "#{event} trigger failed with error => #{e}"
end

#valid_api_key?Boolean

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


118
119
120
121
122
# File 'lib/shipyrd/client.rb', line 118

def valid_api_key?
  raise ArgumentError, "ENV['SHIPYRD_API_KEY'] is not configured, disabling" unless api_key

  true
end

#validate_configurationObject



108
109
110
111
112
113
114
115
116
# File 'lib/shipyrd/client.rb', line 108

def validate_configuration
  valid_api_key?

  true
rescue ArgumentError => e
  logger.info(e.to_s)

  false
end