How to power On/Off host with Rest api?

> From my Java Rest client I'm able to connect to a secure (basic http auth)
> Foreman instance and create new hosts but can't figure out a way to power
> On/Off a host.
>
> No luck with mimicking the browser request [2] either from java or curl.
> It seems like I need to first login and somehow obtain authenticity_token
> for subsequent requests.
>
>
> Foreman logs
>
> 1. Brower request (working): *
>
> Started POST "/hosts/vnguyentest102/power?power_action=start" for
> 10.3.234.230 at Fri Mar 08 11:01:44 -0500 2013
> Processing by HostsController#power as HTML
> Parameters:
> {"authenticity_token"=>"6pm5VCrOFd1AKbJTzSHOEEKh3HlSJjlK3VJ8ntFx2I=",
> "power_action"=>"start", "id"=>"vnguyentest102"}
> Redirected to https://
*****/hosts/vnguyentest102
> Completed 302 Found in 810ms
>
> 2. Rest api - PUT (not working)
>
> Started PUT "/hosts/vnguyentest102/power?power_action=start" for
> 10.3.234.230 at Fri Mar 08 12:20:00 -0500 2013
> Processing by HostsController#power as HTML
> Parameters: {"power_action"=>"start", "id"=>"vnguyentest102"}
> Redirected to https://******/users/login
> Completed 302 Found in 35ms
>
> 3. Rest api - POST (not working)
>
> Started POST "/hosts/vnguyentest102/power?power_action=start" for
> 10.3.234.230 at Fri Mar 08 12:45:36 -0500 2013
> ActionController::RoutingError (No route matches
> "/hosts/vnguyentest102/power"):
>
>
>
how did you execute it? can you provide the full curl line?

Ohad

··· On Fri, Mar 8, 2013 at 9:26 PM, Viet Nguyen wrote:


You received this message because you are subscribed to the Google Groups
"Foreman users" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to foreman-users+unsubscribe@googlegroups.com.
To post to this group, send email to foreman-users@googlegroups.com.
Visit this group at http://groups.google.com/group/foreman-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.

Hello every one,

Is there any updates about this topic ?

I'm using foreman with different compute resources (Libvirt, Ec2, …) as
the main Hub and management tool and would also like to power UP / Down
some Hosts via the API and scheduled cmds… but faced the same issue as
Viet.

What can I provide to you to go forward ?

Thanks a lot in advance :slight_smile:

··· Le jeudi 28 mars 2013 08:58:26 UTC+1, ohad a écrit : > > > > On Fri, Mar 8, 2013 at 9:26 PM, Viet Nguyen <opus...@gmail.com > > wrote: > >> From my Java Rest client I'm able to connect to a secure (basic http >> auth) Foreman instance and create new hosts but can't figure out a way to >> power On/Off a host. >> >> No luck with mimicking the browser request [2] either from java or curl. >> It seems like I need to first login and somehow obtain authenticity_token >> for subsequent requests. >> >> >> Foreman logs >> >> *1. Brower request (working): * >> >> Started POST "/hosts/vnguyentest102/power?power_action=start" for >> 10.3.234.230 at Fri Mar 08 11:01:44 -0500 2013 >> Processing by HostsController#power as HTML >> Parameters: >> {"authenticity_token"=>"6pm5VCrOFd1AKbJTzSHOEEKh3HlSJjlK3VJ8ntFx2I=", >> "power_action"=>"start", "id"=>"vnguyentest102"} >> Redirected to https://******/hosts/vnguyentest102 >> Completed 302 Found in 810ms >> >> *2. Rest api - PUT (not working)* >> >> Started PUT "/hosts/vnguyentest102/power?power_action=start" for >> 10.3.234.230 at Fri Mar 08 12:20:00 -0500 2013 >> Processing by HostsController#power as HTML >> Parameters: {"power_action"=>"start", "id"=>"vnguyentest102"} >> Redirected to https://******/users/login >> Completed 302 Found in 35ms >> >> *3. Rest api - POST (not working)* >> >> Started POST "/hosts/vnguyentest102/power?power_action=start" for >> 10.3.234.230 at Fri Mar 08 12:45:36 -0500 2013 >> ActionController::RoutingError (No route matches >> "/hosts/vnguyentest102/power"): >> >> >> > how did you execute it? can you provide the full curl line? > > Ohad > >> -- >> You received this message because you are subscribed to the Google Groups >> "Foreman users" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to foreman-user...@googlegroups.com . >> To post to this group, send email to forema...@googlegroups.com >> . >> Visit this group at http://groups.google.com/group/foreman-users?hl=en. >> For more options, visit https://groups.google.com/groups/opt_out. >> >> >> > >

The above suggestion did not work for me, but I found a simpler solution
although thanks Kaliel for pointing me in the right direction.

You really only need to PUT to foreman at API endpoint /host/HOST/power
, where HOST is the name of your server in foreman. and you need to PUT
power_action = start or stop. In my case since I am writing in python I
create the json from a dictionary as shown below. I hope this helps. The
code below is a simplified version of my code but I think this does the
trick.

I am writing my client in python

import requests
import simplejson as json

data_dict = { "power_action":"stop"}
data_json = json.dumps(data_dict)

foreman_server = "foreman.localdomain" #replace with the fqdn of your
foreman server
server_name = "myserver.localdomain" #replace with the name of the server
you want to power off

username = "bart"
password = "SimpsoN"

url = "https://{}/hosts/{}/power".format(foreman_server,server_name)
#The above would create url =
"https://foreman.localdomain/hosts/myserver.localdomain/power"

headers = {'Content-type': 'application/json', 'Accept':
'application/json'}
response = requests.put(url,auth=(username,password),
data=data_json, headers=headers,verify=False)

I hope this helps. My virtualization solution is ESX Vmware so I don't know
if this applies to other virtualization backends.

/Jason

··· On Thursday, June 6, 2013 5:41:29 PM UTC+2, Kaliel Aeolian wrote: > > For the record, we finally got it working here (after spending a little > time digging through the source). > > In your 'compute_attributes' section, add 'power_action' : 'start'. This > parameter takes two possible actions, 'start', and 'stop'. > > > On Sunday, May 12, 2013 3:47:03 AM UTC-7, xakraz wrote: >> >> Hello every one, >> >> Is there any updates about this topic ? >> >> I'm using foreman with different compute resources (Libvirt, Ec2, ...) as >> the main Hub and management tool and would also like to power UP / Down >> some Hosts via the API and scheduled cmds... but faced the same issue as >> Viet. >> >> What can I provide to you to go forward ? >> >> >> Thanks a lot in advance :) >> >> >> >> Le jeudi 28 mars 2013 08:58:26 UTC+1, ohad a écrit : >>> >>> >>> >>> On Fri, Mar 8, 2013 at 9:26 PM, Viet Nguyen wrote: >>> >>>> From my Java Rest client I'm able to connect to a secure (basic http >>>> auth) Foreman instance and create new hosts but can't figure out a way to >>>> power On/Off a host. >>>> >>>> No luck with mimicking the browser request [2] either from java or >>>> curl. It seems like I need to first login and somehow obtain >>>> authenticity_token for subsequent requests. >>>> >>>> >>>> Foreman logs >>>> >>>> *1. Brower request (working): * >>>> >>>> Started POST "/hosts/vnguyentest102/power?power_action=start" for >>>> 10.3.234.230 at Fri Mar 08 11:01:44 -0500 2013 >>>> Processing by HostsController#power as HTML >>>> Parameters: >>>> {"authenticity_token"=>"6pm5VCrOFd1AKbJTzSHOEEKh3HlSJjlK3VJ8ntFx2I=", >>>> "power_action"=>"start", "id"=>"vnguyentest102"} >>>> Redirected to https://******/hosts/vnguyentest102 >>>> Completed 302 Found in 810ms >>>> >>>> *2. Rest api - PUT (not working)* >>>> >>>> Started PUT "/hosts/vnguyentest102/power?power_action=start" for >>>> 10.3.234.230 at Fri Mar 08 12:20:00 -0500 2013 >>>> Processing by HostsController#power as HTML >>>> Parameters: {"power_action"=>"start", "id"=>"vnguyentest102"} >>>> Redirected to https://******/users/login >>>> Completed 302 Found in 35ms >>>> >>>> *3. Rest api - POST (not working)* >>>> >>>> Started POST "/hosts/vnguyentest102/power?power_action=start" for >>>> 10.3.234.230 at Fri Mar 08 12:45:36 -0500 2013 >>>> ActionController::RoutingError (No route matches >>>> "/hosts/vnguyentest102/power"): >>>> >>>> >>>> >>> how did you execute it? can you provide the full curl line? >>> >>> Ohad >>> >>>> -- >>>> You received this message because you are subscribed to the Google >>>> Groups "Foreman users" group. >>>> To unsubscribe from this group and stop receiving emails from it, send >>>> an email to foreman-user...@googlegroups.com. >>>> To post to this group, send email to forema...@googlegroups.com. >>>> Visit this group at http://groups.google.com/group/foreman-users?hl=en. >>>> For more options, visit https://groups.google.com/groups/opt_out. >>>> >>>> >>>> >>> >>>

This new power API is brand new in Foreman 1.3.0, thanks to Daniel
Lobato for contributing it. It's designed to be usable on either
virtual or physical systems with a BMC configured in Foreman.

I'm happy to see it's useful, thanks for the update!

··· -- Dominic Cleal Red Hat Engineering

On 16/09/13 18:41, Jason Viloria wrote:

The above suggestion did not work for me, but I found a simpler solution
although thanks Kaliel for pointing me in the right direction.

You really only need to PUT to foreman at API endpoint
/host/HOST/power , where HOST is the name of your server in foreman.
and you need to PUT power_action = start or stop. In my case since I am
writing in python I create the json from a dictionary as shown below. I
hope this helps. The code below is a simplified version of my code but I
think this does the trick.

I am writing my client in python

import requests
import simplejson as json

data_dict = { “power_action”:“stop”}
data_json = json.dumps(data_dict)

foreman_server = “foreman.localdomain” #replace with the fqdn of your
foreman server
server_name = “myserver.localdomain” #replace with the name of the
server you want to power off

username = "bart"
password = “SimpsoN”

url = “https://{}/hosts/{}/power”.format(foreman_server,server_name)
#The above would create url =
https://foreman.localdomain/hosts/myserver.localdomain/power

headers = {‘Content-type’: ‘application/json’, ‘Accept’:
‘application/json’}
response = requests.put(url,auth=(username,password),
data=data_json, headers=headers,verify=False)

I hope this helps. My virtualization solution is ESX Vmware so I don’t
know if this applies to other virtualization backends.

/Jason
On Thursday, June 6, 2013 5:41:29 PM UTC+2, Kaliel Aeolian wrote:

For the record, we finally got it working here (after spending a
little time digging through the source).

In your 'compute_attributes' section, add 'power_action' : 'start'.
This parameter takes two possible actions, 'start', and 'stop'.


On Sunday, May 12, 2013 3:47:03 AM UTC-7, xakraz wrote:

    Hello every one,

    Is there any updates about this topic ?

    I'm using foreman with different compute resources (Libvirt,
    Ec2, ...) as the main Hub and management tool and would also
    like to power UP / Down some Hosts via the API and scheduled
    cmds... but faced the same issue as Viet.

    What can I provide to you to go forward ?


    Thanks a lot in advance :)



    Le jeudi 28 mars 2013 08:58:26 UTC+1, ohad a �crit :



        On Fri, Mar 8, 2013 at 9:26 PM, Viet Nguyen >             <opus...@gmail.com> wrote:

            From my Java Rest client I'm able to connect to a secure
            (basic http auth) Foreman instance and create new hosts
            but can't figure out a way to power On/Off a host.

            No luck with mimicking the browser request [2] either
            from java or curl.  It seems like I need to first login
            and somehow obtain authenticity_token for subsequent
            requests. 


            Foreman logs

            *1. Brower request (working):  *

            Started POST
            "/hosts/vnguyentest102/power?power_action=start" for
            10.3.234.230 at Fri Mar 08 11:01:44 -0500 2013
              Processing by HostsController#power as HTML
              Parameters:
            {"authenticity_token"=>"6pm5VCrOFd1AKbJTzSHOEEKh3HlSJjlK3VJ8ntFx2I=",
            "power_action"=>"start", "id"=>"vnguyentest102"}
            Redirected to https://******/hosts/vnguyentest102
            Completed 302 Found in 810ms

            *2. Rest api - PUT (not working)*

            Started PUT
            "/hosts/vnguyentest102/power?power_action=start" for
            10.3.234.230 at Fri Mar 08 12:20:00 -0500 2013
              Processing by HostsController#power as HTML
              Parameters: {"power_action"=>"start",
            "id"=>"vnguyentest102"}
            Redirected to https://******/users/login
            Completed 302 Found in 35ms

            *3. Rest api - POST (not working)*

            Started POST
            "/hosts/vnguyentest102/power?power_action=start" for
            10.3.234.230 at Fri Mar 08 12:45:36 -0500 2013
            ActionController::RoutingError (No route matches
            "/hosts/vnguyentest102/power"):
             


        how did you execute it? can you provide the full curl line?

        Ohad 

            -- 
            You received this message because you are subscribed to
            the Google Groups "Foreman users" group.
            To unsubscribe from this group and stop receiving emails
            from it, send an email to foreman-user...@googlegroups.com.
            To post to this group, send email to
            forema...@googlegroups.com.
            Visit this group at
            http://groups.google.com/group/foreman-users?hl=en
            <http://groups.google.com/group/foreman-users?hl=en>.
            For more options, visit
            https://groups.google.com/groups/opt_out
            <https://groups.google.com/groups/opt_out>.


You received this message because you are subscribed to the Google
Groups “Foreman users” group.
To unsubscribe from this group and stop receiving emails from it, send
an email to foreman-users+unsubscribe@googlegroups.com.
To post to this group, send email to foreman-users@googlegroups.com.
Visit this group at http://groups.google.com/group/foreman-users.
For more options, visit https://groups.google.com/groups/opt_out.