Skip to content

Commit

Permalink
Don't assume you're using ShipIt
Browse files Browse the repository at this point in the history
  • Loading branch information
mebezac committed Jan 4, 2018
1 parent 76925b5 commit 2f0ada7
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.3.1
2.3.5
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Doggy manages your DataDog dashboards, alerts, monitors, and screenboards.

## Installation

Create a new git repo with an empty `objects` folder.

Add this line to your Gemfile:

```ruby
Expand Down Expand Up @@ -43,10 +45,35 @@ If you're feeling adventurous, just put plaintext `secrets.json` in your root ob
}
```

## Configuration

#### Environment variables

Export the `ENABLE_DEPLOY_EVENT` environment variable and `doggy` will pick it up automatically.

#### json

You can also define your config in `config.json` in your root object store like this:

```json
{
"enable_deploy_event": true
}
```

## Usage

```bash
# Generates a deploy event with the current SHA of your git repo.
# This may be required before your first `doggy sync` if you get an error like:
# NoMethodError: undefined method `[]' for nil:NilClass
# doggy/lib/doggy/model.rb:137:in `current_sha'
# Doing this implies that your current
$ doggy generate_deploy_event

# Syncs local changes to Datadog since last deploy.
# ONLY pushes any changed objects/deletes any deleted objects.
# Does NOT pull down any changes from Datadog.
$ doggy sync

# Download items. If no ID is given it will download all the items managed by dog.
Expand Down
12 changes: 11 additions & 1 deletion lib/doggy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ def application_key
ENV['DATADOG_APP_KEY'] || secrets['datadog_app_key']
end

def enable_deploy_event
ENV['ENABLE_DEPLOY_EVENT'] || config['enable_deploy_event']
end

def resolve_path(path)
path = Pathname.new(path)
curr_dir = Pathname.new(Dir.pwd)
Expand All @@ -70,7 +74,6 @@ def resolve_path(path)
(curr_dir.expand_path(resolved + path) + path).to_s
end


protected

def secrets
Expand All @@ -79,4 +82,11 @@ def secrets
JSON.parse(raw)
end
end

def config
@config ||= begin
raw = File.read(repo_root.join('config.json'))
JSON.parse(raw)
end
end
end
12 changes: 11 additions & 1 deletion lib/doggy/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ def sync
CLI::Push.new.sync_changes
end

desc "generate_deploy_event", "Manually generate a deploy event with the current SHA"
long_desc <<-D
Creates a deploy event in Datadog with the current SHA.
This may be required if you're trying to run 'sync' for the first time,
and therefore have no last deployed SHA to compare to.
D

def generate_deploy_event
CLI::Push.new.generate_deploy_event
end

desc "push [IDs]", "Hard pushes objects to Datadog"
long_desc <<-D
Pushes objects to Datadog. You can provide list of IDs to scope it to certain objects,
Expand Down Expand Up @@ -79,4 +90,3 @@ def edit(id)
end
end
end

6 changes: 5 additions & 1 deletion lib/doggy/cli/push.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ def sync_changes
resource.save
end
end
Doggy::Model.emit_shipit_deployment
generate_deploy_event
end

def generate_deploy_event
Doggy::Model.emit_deployment_event
end

def push_all(ids)
Expand Down
21 changes: 13 additions & 8 deletions lib/doggy/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,20 +131,26 @@ def accepted_response(code, accepted_errors = nil)
def current_sha
now = Time.now.to_i
month_ago = now - 3600 * 24 * 30
result = request(:get, "https://app.datadoghq.com/api/v1/events?start=#{ month_ago }&end=#{ now }&tags=audit,shipit")
result = request(:get, "https://app.datadoghq.com/api/v1/events?start=#{ month_ago }&end=#{ now }&tags=audit,doggy_deploy")
result['events'][0]['text'] # most recetly deployed SHA
end

def emit_shipit_deployment
return unless ENV['SHIPIT']
def emit_deployment_event
return unless Doggy.enable_deploy_event

repo = Rugged::Repository.new(Doggy.object_root.parent.to_s)
ref = repo.head
revision = ref.target.oid
author = ref.target.author[:email] || ref.target.author[:name]
Doggy.ui.say "Creating Doggy Deploy Event by #{author}, with sha = #{revision}"

request(:post, 'https://app.datadoghq.com/api/v1/events', {
title: "ShipIt Deployment by #{ENV['USER']}",
text: ENV['REVISION'],
tags: %w(audit shipit),
title: "Doggy Deployment by #{author}",
text: revision,
tags: %w(audit doggy_deploy),
date_happened: Time.now.to_i,
priority: 'normal',
source_type_name: 'shipit'
source_type_name: 'doggy'
}.to_json)
end

Expand Down Expand Up @@ -230,4 +236,3 @@ def request(method, uri, body = nil)
end
end # Model
end # Doggy

0 comments on commit 2f0ada7

Please sign in to comment.