Coder Social home page Coder Social logo

fleet-api's Introduction

NOTE

This repo is no longer being maintained. Users are welcome to fork it, but we make no warranty of its functionality.

fleet-api

Gem Version Circle CI

Provides a Ruby wrapper around the CoreOS Fleet API.

The client allows programmatic access to most of the fleetctl commands including the ability to load, start, stop, unload and destroy unit files.

Important Note: As of version 1.0.0, this gem is now using the official Fleet REST API. Previous versions of this gem communicated with Fleet by reading/writing directly from/to the etcd key-value store. While this approach was functional, it was extremely brittle due to the fact that we were essentially using a private API. Now that the Fleet API has hit version 1.0 and is presumably stable it makes more sense to leverage the official API.

Users migrating from an older version of the Gem will simply need to make sure they configure it with the Fleet API endpoint instead of the etcd API endpoint.

Installation

Install the gem directly:

gem install fleet-api

Alternatively, add this line to your application's Gemfile:

gem 'fleet-api', require: 'fleet'

Usage

Configure the URL for the Fleet API:

require 'fleet'

Fleet.configure do |fleet|
  fleet.fleet_api_url = 'http://10.1.42.1:49153'
end

If you don't provide an explicit value for the .fleet_api_url attribute, it will default to using the value of the FLEETCTL_ENDPOINT environment variable (if present) or the socket at unix:///var/run/fleet.sock.

Service Definitions

When submitting a service definition to the Fleet::Client you must convert your unit file into a Ruby hash. Each section in the unit file is represented as a key/value pair in the hash where the key is the name of the section and the value is another hash containing all the statements for that section.

For example, look at the following unit file.

[Unit]
Description=Useless infinite loop

[Service]
ExecStart=/bin/bash -c "while true; do sleep 1; done"

This unit file would be represented as the following Ruby hash.

{
  'Unit' => {
    'Description' => 'Useless infinite loop'
  },
  'Service' => {
    'ExecStart' => "/bin/bash -c \"while true; do sleep 1; done\""
  }
}

If you need mutiple values for a single statement (like multiple ExecStart instructions) you can use an array of strings:

{
  'Unit' => {
    'Description' => 'Useless infinite loop'
  },
  'Service' => {
    'ExecStart' => ["/bin/bash -c \"while true; do sleep 1; done\"", "some other command"]
  }
}

Submitting a Unit File

Equivalent of fleetctl submit:

service = {
  'Unit' => {
    'Description' => 'Useless infinite loop'
  },
  'Service' => {
    'ExecStart' => "/bin/bash -c \"while true; do sleep 1; done\""
  }
}

client = Fleet.new
client.submit('forever.service', service)

Note that the name you pass-in as the first parameter to the .submit method should end in ".service"

Loading a Unit File

Equivalent of fleetctl load:

client = Fleet.new
client.load('forever.service')

Starting a Service

Equivalent of fleetctl start:

client = Fleet.new
client.start('forever.service')

Stopping a Service

Equivalent of fleetctl stop:

client = Fleet.new
client.stop('forever.service')

Unloading a Unit File

Equivalent of fleetctl unload:

client = Fleet.new
client.unload('forever.service')

Destroying a Service

Equivalent of fleetctl destroy:

client = Fleet.new
client.destroy('forever.service')

Listing Services

Equivalent of fleetctl list-units:

client = Fleet.new
client.list

Retrieving Service Status

Equivalent of fleetctl status:

client = Fleet.new
client.get_unit_state('forever.service')

Retrieves current status of a unit file:

client = Fleet.new
client.status('forever.service')

Retrieving a Unit File

Retrieves contents and current state of a unit file:

client = Fleet.new
client.get_unit_file('foo.service')

fleet-api's People

Contributors

bdehamer avatar pigri avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

fleet-api's Issues

757: unexpected token at '404 page not found'

Hey ,
I just upgraded fleet-api to version 1.1.0 from 0.9.0 and I it looks like there are some issues.
I am using clinet.status to get the status of the unit and also to know if unit already exist
here is my status method

  def status
    @ustatus ||= @ustatus = "none"
    begin
     @ustatus=fleet.status(unitname)
    rescue Fleet::NotFound
      nil
    end
  end

When trying to use 1.1.0 I keep getting Json parser error - quick look at the code shows that you are trying to parse all errors - but 404 error is not parable (probably not in a json format).
here is the exception :

 757: unexpected token at '404 page not found'
/root/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/json-1.8.2/lib/json/common.rb:155:in `parse'
/root/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/json-1.8.2/lib/json/common.rb:155:in `parse'
/root/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/fleet-api-1.1.0/lib/fleet/request.rb:51:in `raise_error'
/root/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/fleet-api-1.1.0/lib/fleet/request.rb:31:in `request'
 /root/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/fleet-api-1.1.0/lib/fleet/request.rb:11:in `block (2 levels) in <module:Request>'
/root/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/fleet-api-1.1.0/lib/fleet/client/unit.rb:12:in `get_unit'

So the question is why is it not returning Fleet::NotFound like in 0.9.0? and how can we change "404 not found" to be parseable by json parser ?

Get list of all units

Hello,
Is there a way to get all units or all unit files?

Basically looking for the equiv of:
list-units
and
list-unit-files

This would give me something to iterate if I didn't already know the name of the units

Neither Fleet::Client#get_unit or Fleet::Client#status return active state

Means that the only way to see the actual state for a service is to parse the entire list of all launched units (which could be quite large in a big cluster).

Ideally an instance unit doesn't show up in the equivalent of "fleetctl list-unit-files", only in "fleetctl list-units".

Maybe a distinction in the API client would provide more clarity, like #get_unit_file (current behavior for get_unit), and #get_unit would return only active units?

Fleet 0.8.3 & Etcd 0.4.6

I'm having trouble launching a unit using Fleet 0.8.3 & Etcd 0.4.6. I've noticed that the etcd store is different from units launched locally with fleetctl than with fleet-api. Some keys appear to be missing.

$ etcdctl ls /_coreos.com/fleet/job/primes.service
/_coreos.com/fleet/job/primes.service/target
/_coreos.com/fleet/job/primes.service/job-state
/_coreos.com/fleet/job/primes.service/object
/_coreos.com/fleet/job/primes.service/target-state
$ etcdctl ls /_coreos.com/fleet/job/p12/      
/_coreos.com/fleet/job/p12/target-state

Is this expected behavior? Does fleet-api write each of these files or are some byproducts of the post?

Thanks,
Ken

Unit interactions are a struggle

Currently if I want to update a unit it requires bypassing the ServiceDefinition class entirely as it won't handle the additional required attributes, and the Fleet::Client::Update#update_unit method doesn't take a ServiceDefinition object. So as it stands, it's necessary to handle both formats and convert between the two.

I propose either:

  1. Getting rid of ServiceDefinition entirely and just operate on the fleet hash directly
  2. make all methods currently in Fleet::Client::Unit move to ServiceDefinition, which would require probably a more significant refactor than the first option.

I'm happy to submit a PR if you agree with either of these, or have a better idea.

JSON vs Ruby Hash

How do I define a unit for a multi-ExecStart service?

The Fleet API takes a tuplet like:

"options": [{"section": "Service", "name": "ExecStart", "value": "/usr/bin/sleep 3000"}]

This allows for duplicate "name" keys in a section, allowing me to do something like:

"options": [{"section": "Service", "name": "ExecStart", "value": "/usr/bin/sleep 3000"}]
                 {"section": "Service", "name": "ExecStart", "value": "/usr/bin/echo Done."}]

Unfortunately, the fleet-api service definition doesn't allow for this:

{
  'Service' => {
    'ExecStart' => "/usr/bin/sleep 3000"
    'ExecStart' => "/usr/bin/echo Done."
  }
}

Unlike a JSON object that does allow multiple "keys" as it is not an associative array, a ruby Hash is an associative array... unless I'm missing something here? (which is entirely possible)

Thanks!

Does not distinguish between templates and instance units

If I load a unit as a template using the format [email protected], fleet-api creates a job for it, which fleet then loops and complains about on whatever machine it scheduled to. If a unit is identified as a template it should not create a job. (I'm assuming this is the extent of the problem)

Launching instances of this template fails after this (jobs are created but nothing happens and they don't show up in the output of fleetctl list-units

This happens on both 0.8.3 and 0.9.0

log output:

ERROR generator.go:51: Failed fetching current unit states: Unit name [email protected] is not valid.

404 page not found

With the switch from faraday to excon, I'm now seeing a "Error loading unit file 757: unexpected token at '404 page not found" message when making load requests.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.