Search and get hosts on GCP GCE by metadata
MIT License
Search hosts on GCP GCE
gem install gce-host
This gems uses metadata of GCE resources.
You can configure, but basically use roles
key for roles.
You can manage roles of a host, and search hosts having a specified role using thease metadata with this gem.
You can write a configuration file located at /etc/sysconfig/gce-host
or /etc/default/gce-host
(You can configure this path by GCE_HOST_CONFIG_FILE
environment variable), or as environment variables:
GOOGLE API parameters:
compute_engine
, service_account
, authorized_user
, and application_default
is available. The default is type
in GOOGLE_APPLICATION_CREDENTIALS
if it exists, otherwise compute_engine
.~/.config/gcloud/application_default_credentials.json
.project_id
in GOOGLE_APPLICATION_CREDENTIALS
if it exists (typically, available in service acaccount json)gce-host parameters:
roles
ARRAY_VALUE_DELIMITER
(default: ,
)ROLE_VALUE_DELIMITER
(default :
)GCE::Host.new(role: 'admin:ami')
and also GCE::Host.new(role1: 'admin')
returns this host:
service,status
.ARRAY_VALUE_DELIMITER
(default: ,
),
info
, debug
, error
. The default is info
.See example.conf
$ gce-host -j
{"hostname":"gce-host-db","roles":["foo","db:test"],"zone":"asia-northeast1-a","service":"gce-host","status":"active","tags":["master"],"instance_id":"4263858691219514807","private_ip_address":"10.240.0.6","public_ip_address":"104.198.89.55","creation_timestamp":"2016-11-22T06:51:04.650-08:00","state":"RUNNING"}
{"hostname":"gce-host-web","roles":["foo","web:test"],"zone":"asia-northeast1-a","service":"gce-host","status":"reserve","tags":["standby"],"instance_id":"8807276062743061943","private_ip_address":"10.240.0.5","public_ip_address":"104.198.87.6","creation_timestamp":"2016-11-22T06:51:04.653-08:00","state":"RUNNING"}
$ gce-host
gce-host-db
gce-host-web
$ gce-host --role1 db
gce-host-db
$ gce-host --role web:test
gce-host-web
$ gce-host --pretty-json
[
{
"hostname": "gce-host-db",
"roles": [
"foo",
"db:test"
],
"zone": "asia-northeast1-a",
"service": "gce-host",
"status": "active",
"tags": [
"master"
],
"instance_id": "4263858691219514807",
"private_ip_address": "10.240.0.6",
"public_ip_address": "104.198.89.55",
"creation_timestamp": "2016-11-22T06:51:04.650-08:00",
"state": "RUNNING"
},
{
"hostname": "gce-host-web",
"roles": [
"foo",
"web:test"
],
"zone": "asia-northeast1-a",
"service": "gce-host",
"status": "reserve",
"tags": [
"standby"
],
"instance_id": "8807276062743061943",
"private_ip_address": "10.240.0.5",
"public_ip_address": "104.198.87.6",
"creation_timestamp": "2016-11-22T06:51:04.653-08:00",
"state": "RUNNING"
}
]
$ bin/gce-host --help
Usage: gce-host [options]
--hostname one,two,three name or private_dns_name
-r, --role one,two,three role
--r1, --role1 one,two,three role1, 1th part of role delimited by :
--r2, --role2 one,two,three role2, 2th part of role delimited by :
--r3, --role3 one,two,three role3, 3th part of role delimited by :
--state one,two,three filter with instance state (default: running)
-a, --all list all hosts (remove default filter)
--private-ip, --ip show private ip address instead of hostname
--public-ip show public ip address instead of hostname
-i, --info show host info
-j, --jsonl show host info in line delimited json
--json show host info in json
--pretty-json show host info in pretty json
--debug debug mode
-h, --help show help
require 'gce-host'
hosts = GCE::Host.new(role: 'db:test')
hosts.each do |host|
puts host
end
See http://sonots.github.io/gce-host/frames.html.
See CHANGELOG.md for details.
NOTE: Currently, mock is not supported yet. So, you have to create your own gcloud account, and instances.
Configure .env file as
AUTH_METHOD=service_account
GOOGLE_APPLICATION_CREDENTIALS=service_account.json
GOOGLE_PROJECT=XXXXXXXXXXXXX
OPTIONAL_STRING_KEYS=service,status
OPTIONAL_ARRAY_KEYS=tags
ROLE_MAX_DEPTH=5
STATUS=state
Prepare service_account.json
.
Install terraform and run to create instances for tests
$ brew install terraform
$ env $(cat .env) terraform plan
$ env $(cat .env) terraform apply
Run test
$ bundle exec rspec
After working, destory instances by commenting out terraform.tf
and apply.
bundle exec rake release
bundle exec yard
See LICENSE