Project

General

Profile

Actions

Feature #945

closed

foreman CLI

Added by Ohad Levy almost 13 years ago. Updated over 10 years ago.

Status:
Closed
Priority:
Normal
Assignee:
Category:
-
Target version:
-
Difficulty:
Triaged:
Team Backlog:
Fixed in Releases:
Found in Releases:
In Kanboard:

Description

All of the operations over the UI could be done via the API as well.

Foreman should have an command line interface, where basic functionaily could be performed:

list hosts, inventory(facts) and their report status.
create / update / delete hosts
...

an example for api usage could be found at the API page, or an example scripts :

Related issues 3 (0 open3 closed)

Related to Foreman - Refactor #1173: remove legacy query interfaceClosedOhad LevyActions
Related to Foreman - Feature #1222: API enhancementClosed10/12/2011Actions
Related to Foreman - Bug #1245: API bug with search a dot in front of domain when pulling a host list returns the full hostlist of all hosts rather than matching the domain stringFeedback10/21/2011Actions
Actions #1

Updated by Ohad Levy almost 13 years ago

Foreman has a legacy query script1 which would probably needs to be part of the CLI as well, once exists, we could remove the old API.

[1] - source:extras/query

Actions #2

Updated by Brian Gupta almost 13 years ago

  • Assignee set to Brian Gupta
Actions #3

Updated by Brian Gupta almost 13 years ago

Ok so I have a script here: https://gist.github.com/994819

Note the "collections" array isn't used yet, but I was thinking to turn it into a hash with {"collectionname == path", "description"}, which would really enable me to remove a lot of theĀ repetitionĀ in the get.opts and the get_list calls.

I started thinking a bit further, and was wondering if it would make sense to add another "collection" to the API, that is called from "/API/collections" that returns the hash I am describing?

Thoughts?

P.S. - When I say collections, I am talking about a REST collection that when you use a GET method, it returns a list of "elements" e.g. GET /hostgroups returns a list of hostgroups

Actions #4

Updated by Brian Gupta almost 13 years ago

I maybe overthinking things, but I could see the /API pardygm extended further:

GET /API returning a list of what you can get from it. e.g.

/API/collections - returns a list of REST collections the API supports
/API/versions - returns a list of API versions

you could even do nested stuff like:

/API/collections/hostlist - return the URLs and methods that hostlist supports

with API versioning we could do GET /v1.0/API/collections

Actions #5

Updated by Brian Gupta almost 13 years ago

Ohad suggested we build a "lib of objects" like has been down for smart-proxy: https://github.com/ohadlevy/foreman/blob/develop/lib/proxy_api.rb

Actions #6

Updated by Brian Gupta almost 13 years ago

Updated Ohad's optimization, by converting the Hashes to arrays, and updating code to address arrays vs hashes. https://gist.github.com/996702

Actions #7

Updated by Ohad Levy over 12 years ago

and now with hostgroup as defaults attributes its as simple as https://gist.github.com/1229194

Actions #8

Updated by Brian Gupta over 12 years ago

  • % Done changed from 0 to 10

I have not yet refactored to accomodate "lib of objects", but at this point I have a basic script that can query most collections and filter on a few. (It's now useful to us, and I'm hopin it might be useful to others.)

It has nothing hardcoded that is site specific, and takes arguments for URL, username and password. (I have tested with SSL and basicauth enabled. It should work without auth and http, but I don't have a test server setup anymore without them).

Error handling for options parsing is nonexistant. I need to figure out how to handle whether or not to print in JSON or not.

Here is the script, comments welcome: https://gist.github.com/1271734

Actions #9

Updated by Brian Gupta over 12 years ago

Updated again. Fixed some issues working with basic auth, and refactored the get/searchcollections, to be more elegant, in addition, I now delete the "options hash" elements as the instance variables are instantiated (and all class variables were converted to instance variables). In addition the output now defaults to plain text and one needs to use --json to get the results in "pretty" JSON.

Please check it out, and give me your thoughts (in this issue): https://gist.github.com/1271734

Also as an aside, I still think we need a fully functionally command line tool that can do everything including provisioning. (Similar to Chef's "knife" utility). I think a good name would be "hammer".

One open question, is a name for this read-only script. I am using foremancli as a working name, but that implies a bit more functionality than I am providing. As it stands this script only lists collections and searches collections. ('hammer" will be the fully functional tool that can read and set any data, as well as trigger provisioning.)

Actions #10

Updated by Brian Gupta over 12 years ago

Ohad Levy wrote:

and now with hostgroup as defaults attributes its as simple as https://gist.github.com/1229194

Could you please elaborate? We don't use hostgroups, so I am not sure, what this does, or how to incorporate this functionality into the script.

Actions #11

Updated by Ohad Levy over 12 years ago

Brian Gupta wrote:

Ohad Levy wrote:

and now with hostgroup as defaults attributes its as simple as https://gist.github.com/1229194

Could you please elaborate? We don't use hostgroups, so I am not sure, what this does, or how to incorporate this functionality into the script.

This simply creates a new host and provision it.
assuming that the host group contain all important attributes (e.g. vm properies(mem, vcpu etc) network, os attributes etc).

Actions #12

Updated by Brian Gupta over 12 years ago

Ohad starting coding a framework for high level objects (based on active resource) here: https://github.com/ohadlevy/stacker/blob/master/lib/foreman.rb

Actions #13

Updated by Brian Gupta over 12 years ago

Ok, now has another option to support any custom nested requests like /hosts.fqdn/facts

--custom COLLECTION          Custom COLLECTION string, see: http://theforeman.org/projects/foreman/wiki/API

https://gist.github.com/1271734

Actions #14

Updated by Brian Gupta over 12 years ago

Added support for three optional ENVIRONMENT VARIABLES:

FOREMAN_SERVER                  Foreman Server URL
FOREMAN_USER Foreman user
FOREMAN_PASSWORD Foreman password

CLI options take precedence over ENV_VARS.

https://gist.github.com/1271734

Actions #15

Updated by Brian Gupta over 12 years ago

Basic --status (Foreman server status) has been added: https://gist.github.com/1271734 Currently spits out JSON status report e.g.:

{
"result": "ok",
"version": "0.3",
"db_duration_ms": "1",
"status": 200
}

Not sure what people would like to see here.

Actions #16

Updated by Ohad Levy over 12 years ago

  • Category set to 44
  • Target version set to 0.4
Actions #17

Updated by Brian Gupta over 12 years ago

  • Status changed from New to Closed
  • % Done changed from 10 to 100

Applied in changeset commit:"71ce3e9bbefebffb5dc18a26f9d6b64fd50c2df0".

Actions #18

Updated by Dominic Cleal over 10 years ago

  • Project changed from Foreman to Hammer CLI
  • Category deleted (44)
  • Target version deleted (0.4)
Actions

Also available in: Atom PDF