FIX 完善devops流程

This commit is contained in:
Jasder
2020-07-14 15:06:09 +08:00
parent e74bba9092
commit 6527603e24
21 changed files with 1080 additions and 2619 deletions
+8 -6
View File
@@ -1,23 +1,25 @@
class DevOps::Drone::Ci
attr_reader :host, :username, :password
attr_reader :host, :username, :password, :gitea_username
# host: drone server's ip
# username: drone server's account
# password: drone server's password
# eq:
# DevOps::Drone::Ci.new(@cloud_account.drone_ip, @cloud_account.account, @cloud_account.visible_secret).get_token
def initialize(host, username, password)
@host = host
@username = username
@password = password
def initialize(host, username, password, gitea_username)
@host = host
@username = username
@password = password
@gitea_username = gitea_username
end
def get_token
puts "--------- sshpass -p #{password} ssh -o 'StrictHostKeyChecking no' #{username}@#{host} '#{cmd}'"
`sshpass -p #{password} ssh -o "StrictHostKeyChecking no" #{username}@#{host} "#{cmd}"`
end
private
def cmd
"cd ..; cd var/lib/drone/; sqlite3 database.sqlite; .dump; select user_hash from users where user_login=#{username} "
"cd ..; cd var/lib/drone/; sqlite3 database.sqlite; .dump; select user_hash from users where user_login=#{gitea_username} "
end
end
+1 -2
View File
@@ -33,7 +33,6 @@ class DevOps::Drone::Client
--restart always \
--name drone-agent--#{client_id} \
--net='bridge' \
drone/drone-runner-docker:1
"
drone/drone-runner-docker:1"
end
end
+110
View File
@@ -0,0 +1,110 @@
# @private
class DevOps::Drone::Request
format :json
headers 'Accept' => 'application/json'
parser Proc.new { |body, _| parse(body) }
attr_accessor :private_token
# Converts the response body to an ObjectifiedHash.
def self.parse(body)
body = decode(body)
if body.is_a? Hash
ObjectifiedHash.new body
elsif body.is_a? Array
body.collect! { |e| ObjectifiedHash.new(e) }
elsif body == true
body
else
raise Error::Parsing.new "Couldn't parse a response body"
end
end
# Decodes a JSON response into Ruby object.
def self.decode(response)
begin
JSON.load response
rescue JSON::ParserError
raise Error::Parsing.new "The response is not a valid JSON"
end
end
def get(path, options={})
set_httparty_config(options)
set_private_token_header(options)
validate self.class.get(path, options)
end
def post(path, options={})
set_httparty_config(options)
set_private_token_header(options, path)
validate self.class.post(path, options)
end
def put(path, options={})
set_httparty_config(options)
set_private_token_header(options)
validate self.class.put(path, options)
end
def delete(path, options={})
set_httparty_config(options)
set_private_token_header(options)
validate self.class.delete(path, options)
end
# Checks the response code for common errors.
# Returns parsed response for successful requests.
def validate(response)
# case response.code
# when 400; raise Error::BadRequest.new error_message(response)
# when 401; raise Error::Unauthorized.new error_message(response)
# when 403; raise Error::Forbidden.new error_message(response)
# when 404; raise Error::NotFound.new error_message(response)
# when 405; raise Error::MethodNotAllowed.new error_message(response)
# when 406; raise Error::DataNotAccepted.new error_message(response)
# when 409; raise Error::Conflict.new error_message(response)
# when 500; raise Error::InternalServerError.new error_message(response)
# when 502; raise Error::BadGateway.new error_message(response)
# when 503; raise Error::ServiceUnavailable.new error_message(response)
# end
response.parsed_response
end
# Sets a base_uri and default_params for requests.
# @raise [Error::MissingCredentials] if endpoint not set.
def set_request_defaults(endpoint, private_token, sudo=nil)
raise Error::MissingCredentials.new("Please set an endpoint to API") unless endpoint
@private_token = private_token
self.class.base_uri endpoint
self.class.default_params :sudo => sudo
self.class.default_params.delete(:sudo) if sudo.nil?
end
private
# Sets a PRIVATE-TOKEN header for requests.
# @raise [Error::MissingCredentials] if private_token not set.
def set_private_token_header(options, path=nil)
unless path == '/session'
raise Error::MissingCredentials.new("Please set a private_token for user") unless @private_token
options[:headers] = {'PRIVATE-TOKEN' => @private_token}
end
end
# Set HTTParty configuration
# @see https://github.com/jnunemaker/httparty
def set_httparty_config(options)
if self.httparty
options.merge!(self.httparty)
end
end
def error_message(response)
"Server responded with code #{response.code}, message: #{response.parsed_response.message}. " \
"Request URI: #{response.request.base_uri}#{response.request.path}"
end
end
+5 -3
View File
@@ -33,9 +33,12 @@ class DevOps::Drone::Server
`
end
# TODO 一下代码方便测试,正式环境请移除
# docker rm -f `docker ps -qa`
def generate_cmd
"service docker start; docker run \
"service docker start; docker rm -f `docker ps -qa`; docker run \
-v /var/run/docker.sock:/var/run/docker.sock \
-v /var/lib/drone:/data \
-e DRONE_GITEA_SERVER=#{gitea_url} \
-e DRONE_GITEA_CLIENT_ID=#{client_id} \
-e DRONE_GITEA_CLIENT_SECRET=#{client_secret} \
@@ -48,8 +51,7 @@ class DevOps::Drone::Server
--detach=true \
--name=drone-server-#{client_id} \
--net='bridge' \
drone/drone:1
"
drone/drone:1"
end
private