Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
G
gitlab-runner-do-monitor
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
2
Issues
2
List
Boards
Labels
Service Desk
Milestones
Iterations
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Container Registry
Analytics
Analytics
CI / CD
Code Review
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Server Tools
gitlab-runner-do-monitor
Commits
da31dc35
Commit
da31dc35
authored
Aug 19, 2018
by
Erick Hitter
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Successful API retrieval
parent
91935a4f
Pipeline
#172
failed with stages
in 3 minutes and 5 seconds
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
75 additions
and
11 deletions
+75
-11
config-sample.json
config-sample.json
+1
-1
glrdomon.go
glrdomon.go
+74
-10
No files found.
config-sample.json
View file @
da31dc35
...
...
@@ -3,5 +3,5 @@
"debug"
:
false
,
"api-key"
:
""
,
"threshold"
:
3600
,
"schedule"
:
"
0
10 * * * *"
"schedule"
:
"
* */
10 * * * *"
}
glrdomon.go
View file @
da31dc35
package
main
import
(
"context"
"encoding/json"
"flag"
"io/ioutil"
...
...
@@ -10,7 +11,9 @@ import (
"path/filepath"
"syscall"
"github.com/digitalocean/godo"
"github.com/robfig/cron"
"golang.org/x/oauth2"
)
type
config
struct
{
...
...
@@ -21,6 +24,10 @@ type config struct {
Schedule
string
`json:"schedule"`
}
type
TokenSource
struct
{
AccessToken
string
}
var
(
configPath
string
...
...
@@ -28,8 +35,12 @@ var (
debugDest
string
debug
bool
apiKey
string
threshold
int
schedule
string
client
*
godo
.
Client
)
func
init
()
{
...
...
@@ -54,6 +65,8 @@ func init() {
debugDest
=
cfg
.
DebugDest
debug
=
cfg
.
Debug
apiKey
=
cfg
.
ApiKey
threshold
=
cfg
.
Threshold
schedule
=
cfg
.
Schedule
...
...
@@ -70,29 +83,80 @@ func main() {
logger
.
Println
(
"Test"
)
}
if
authenticate
()
{
startCron
()
}
else
{
sig
<-
syscall
.
SIGTERM
}
authenticate
()
startCron
()
caughtSig
:=
<-
sig
logger
.
Printf
(
"Stopping, got signal %s"
,
caughtSig
)
}
func
authenticate
()
bool
{
return
true
func
authenticate
()
{
tokenSource
:=
&
TokenSource
{
AccessToken
:
apiKey
,
}
oauthClient
:=
oauth2
.
NewClient
(
context
.
Background
(),
tokenSource
)
client
=
godo
.
NewClient
(
oauthClient
)
}
func
(
t
*
TokenSource
)
Token
()
(
*
oauth2
.
Token
,
error
)
{
token
:=
&
oauth2
.
Token
{
AccessToken
:
t
.
AccessToken
,
}
return
token
,
nil
}
func
startCron
()
{
c
:=
cron
.
New
()
c
.
AddFunc
(
schedule
,
check
)
c
.
AddFunc
(
schedule
,
check
Api
)
c
.
Start
()
}
func
check
()
{
logger
.
Println
(
"Check!"
)
func
checkApi
()
{
context
:=
context
.
TODO
()
droplets
,
err
:=
listDroplets
(
context
,
client
)
if
err
!=
nil
{
logger
.
Fatal
(
"Failed to retrieve droplet list"
)
}
for
_
,
droplet
:=
range
droplets
{
go
checkDropletAge
(
droplet
)
}
}
func
listDroplets
(
ctx
context
.
Context
,
client
*
godo
.
Client
)
([]
godo
.
Droplet
,
error
)
{
list
:=
[]
godo
.
Droplet
{}
opt
:=
&
godo
.
ListOptions
{}
for
{
droplets
,
resp
,
err
:=
client
.
Droplets
.
List
(
ctx
,
opt
)
if
err
!=
nil
{
return
nil
,
err
}
for
_
,
d
:=
range
droplets
{
list
=
append
(
list
,
d
)
}
if
resp
.
Links
==
nil
||
resp
.
Links
.
IsLastPage
()
{
break
}
page
,
err
:=
resp
.
Links
.
CurrentPage
()
if
err
!=
nil
{
return
nil
,
err
}
opt
.
Page
=
page
+
1
}
return
list
,
nil
}
func
checkDropletAge
(
droplet
godo
.
Droplet
)
{
logger
.
Print
(
droplet
.
ID
)
logger
.
Print
(
droplet
.
Created
)
}
func
setUpLogger
()
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment