Feature #945
closedforeman CLI
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
...
- Using curl - https://gist.github.com/937501
- Using search - https://gist.github.com/919578
- Creating a new host - https://gist.github.com/913305
Updated by Ohad Levy over 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
Updated by Brian Gupta over 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
Updated by Brian Gupta over 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
Updated by Brian Gupta over 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
Updated by Brian Gupta over 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
Updated by Ohad Levy over 13 years ago
and now with hostgroup as defaults attributes its as simple as https://gist.github.com/1229194
Updated by Brian Gupta about 13 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
Updated by Brian Gupta about 13 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.)
Updated by Brian Gupta about 13 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.
Updated by Ohad Levy about 13 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).
Updated by Brian Gupta about 13 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
Updated by Brian Gupta about 13 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
Updated by Brian Gupta about 13 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.
Updated by Brian Gupta about 13 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.
Updated by Ohad Levy about 13 years ago
- Category set to 44
- Target version set to 0.4
Updated by Brian Gupta about 13 years ago
- Status changed from New to Closed
- % Done changed from 10 to 100
Applied in changeset commit:"71ce3e9bbefebffb5dc18a26f9d6b64fd50c2df0".
Updated by Dominic Cleal about 11 years ago
- Project changed from Foreman to Hammer CLI
- Category deleted (
44) - Target version deleted (
0.4)