Class: Shipyrd::Client
- Inherits:
-
Object
- Object
- Shipyrd::Client
- 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_COMMAND SHIPYRD_COMMIT_MESSAGE SHIPYRD_DESTINATION SHIPYRD_HOST SHIPYRD_HOSTS SHIPYRD_PERFORMER SHIPYRD_RECORDED_AT SHIPYRD_ROLE SHIPYRD_RUNTIME SHIPYRD_SERVICE_VERSION SHIPYRD_SUBCOMMAND SHIPYRD_VERSION ]
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#valid_configuration ⇒ Object
readonly
Returns the value of attribute valid_configuration.
Instance Method Summary collapse
- #commit_message ⇒ Object
- #env ⇒ Object
- #github_username ⇒ Object
-
#initialize(host: ENV["SHIPYRD_HOST"], api_key: ENV["SHIPYRD_API_KEY"], **options) ⇒ Client
constructor
A new instance of Client.
- #parse_host(host) ⇒ Object
- #performer ⇒ Object
- #trigger(event) ⇒ Object
- #valid_api_key? ⇒ Boolean
- #validate_configuration ⇒ Object
Constructor Details
#initialize(host: ENV["SHIPYRD_HOST"], api_key: ENV["SHIPYRD_API_KEY"], **options) ⇒ Client
Returns a new instance of Client.
32 33 34 35 36 37 |
# File 'lib/shipyrd/client.rb', line 32 def initialize(host: ENV["SHIPYRD_HOST"], api_key: ENV["SHIPYRD_API_KEY"], **) @host = parse_host(host) @api_key = api_key @logger = [:logger] || Shipyrd::Logger.new @valid_configuration = validate_configuration end |
Instance Attribute Details
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
2 3 4 |
# File 'lib/shipyrd/client.rb', line 2 def api_key @api_key end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
2 3 4 |
# File 'lib/shipyrd/client.rb', line 2 def host @host end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
2 3 4 |
# File 'lib/shipyrd/client.rb', line 2 def logger @logger end |
#valid_configuration ⇒ Object (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_message ⇒ Object
108 109 110 111 112 113 114 115 116 |
# File 'lib/shipyrd/client.rb', line 108 def = `git show -s --format=%s`.chomp if .length >= 90 "#{[0..90]}..." else end end |
#env ⇒ Object
94 95 96 |
# File 'lib/shipyrd/client.rb', line 94 def env ENV.slice(*ENV_VARS) end |
#github_username ⇒ Object
102 103 104 105 106 |
# File 'lib/shipyrd/client.rb', line 102 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
134 135 136 137 138 139 |
# File 'lib/shipyrd/client.rb', line 134 def parse_host(host) return "https://hooks.shipyrd.io" if host.nil? return host if host.start_with?("https") "https://#{host}" end |
#performer ⇒ Object
98 99 100 |
# File 'lib/shipyrd/client.rb', line 98 def performer github_username.empty? ? env_var("PERFORMER") : "https://github.com/#{github_username}" end |
#trigger(event) ⇒ Object
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 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/shipyrd/client.rb', line 39 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_var("RECORDED_AT"), performer: performer, commit_message: ENV["SHIPYRD_COMMIT_MESSAGE"] || , version: env_var("VERSION"), service_version: env_var("SERVICE_VERSION"), hosts: env_var("HOSTS"), command: env_var("COMMAND"), subcommand: env_var("SUBCOMMAND"), role: env_var("ROLE"), destination: env_var("DESTINATION"), runtime: env_var("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.})" end rescue DestinationBlocked raise rescue => e logger.info "#{event} trigger failed with error => #{e}" end |
#valid_api_key? ⇒ Boolean
128 129 130 131 132 |
# File 'lib/shipyrd/client.rb', line 128 def valid_api_key? raise ArgumentError, "ENV['SHIPYRD_API_KEY'] is not configured, disabling" unless api_key true end |
#validate_configuration ⇒ Object
118 119 120 121 122 123 124 125 126 |
# File 'lib/shipyrd/client.rb', line 118 def validate_configuration valid_api_key? true rescue ArgumentError => e logger.info(e.to_s) false end |