Auto Start / Stop Compute Instances

By | January 3, 2019

I have extended the library of scripts for automatic scheduling. There was already a script to auto Scale BM Database and ATP/ADW instances. New is the autoOnOff script for compute instances.

Based on a pre-defined tag in the namespace “Schedule” you can now automatically power on or off compute instances. This will not delete any of the data, but just powers the VM instance on or off. When compute instances are powered off, this will be done by a “softstop”, this means the instance will gracefully shutdown by sending a shutdown command to the operating system.

You can find the script here:

https://github.com/AnykeyNL/OCI-AutoScale

39 thoughts on “Auto Start / Stop Compute Instances

  1. Manu

    Hello,

    I have one question: you run this script connected on the db system with putty(as it seems in youtube video you have for autoscalling) or you run it from the shell on your laptop with oci cli configured?

    Thank you

    Reply
    1. richard Post author

      Hi,in principle you can run the script from anywhere as long as it can connect to the API service from OCI.

      Reply
  2. Manu

    Thank you for the answer.
    I have managed to succesfully run the script from a docker image, at works perfectly for normal instances but it doesn’t seems to work on DB System, where I am trying to stop/start a node.:(

    Reply
    1. Manu

      I was trying starting/stopping DBS, ATP and ADW but except normal instance I was unsuccesfull

      Reply
    2. richard Post author

      Database Instances can not be stopped. You can start/stop autonomous services, but this different API call then needed for a compute instance.

      Reply
    3. AK

      Hi,
      Could you please share DockerFile for this setup ? i am trying build Image.

      Reply
  3. Pingback: Autoscaling Options in OCI

  4. Maxime

    Hello
    Thank you for your work !
    A little mistake in the README, WeekDay is written, but the script is checking for Weekday

    Reply
  5. AK

    Hi,
    i wouldnt find Tag namespace Schedule, how do i create it?

    Reply
  6. Daniel Dueñas

    Hi, I’m trying to use your script but i have an error, could you please help me?
    It get my regions, then I get this error:

    Traceback (most recent call last):
    File “AutoScaleCompute.py”, line 132, in
    response = oci.pagination.list_call_get_all_results(ComputeClient.list_insta
    nces,compartment_id=compartment.id)
    File “C:\Python\Python37-32\lib\site-packages\oci\pagination\pagination_utils.
    py”, line 203, in list_call_get_all_results
    for response in list_call_get_all_results_generator(list_func_ref, ‘response
    ‘, *list_func_args, **list_func_kwargs):
    File “C:\Python\Python37-32\lib\site-packages\oci\pagination\pagination_utils.
    py”, line 271, in list_call_get_all_results_generator
    call_result = retry.DEFAULT_RETRY_STRATEGY.make_retrying_call(list_func_ref,
    *list_func_args, **list_func_kwargs)
    File “C:\Python\Python37-32\lib\site-packages\oci\retry\retry.py”, line 271, i
    n make_retrying_call
    return func_ref(*func_args, **func_kwargs)
    File “C:\Python\Python37-32\lib\site-packages\oci\core\compute_client.py”, lin
    e 4452, in list_instances
    response_type=”list[Instance]”)
    File “C:\Python\Python37-32\lib\site-packages\oci\base_client.py”, line 229, i
    n call_api
    response = self.request(request)
    File “C:\Python\Python37-32\lib\site-packages\oci\base_client.py”, line 352, i
    n request
    self.raise_service_error(request, response)
    File “C:\Python\Python37-32\lib\site-packages\oci\base_client.py”, line 522, i
    n raise_service_error
    original_request=request)
    oci.exceptions.ServiceError: {‘opc-request-id’: ‘Some code’, ‘code’: ‘N
    otAuthorizedOrNotFound’, ‘message’: ‘Authorization failed or requested resource
    not found.’, ‘status’: 404}

    Reply
    1. richard Post author

      Hi,

      Looks like an authorization issue. Are you trying to do this as an admin user? If not, what policies do you have set for the user?

      Reply
      1. Daniel Dueñas

        Yes i have admin Privileges, i think is because i only have permission on one region and are 3 on the list, because it fails checking one that i dont use

        Reply
        1. richard Post author

          If you just want to go thru one region, change this part of the code:

          regions = identity.list_region_subscriptions(config[“tenancy”]).data
          regionnames = “”
          for region in regions:
          regionnames = regionnames + region.region_name + ” “

          Reply
          1. Daniel Dueñas

            Now, I have this error

            C:\Start-Stop OCI>C:\Python\Python37-32\python.exe AutoScaleCompute.py on
            File “AutoScaleCompute.py”, line 93
            regionnames = regionnames + region.region_name + ” ”
            ^
            IndentationError: expected an indented block

          2. Daniel Dueñas

            The previous error is corrected, but still passing through the other regions, where should i put that part of code?

          3. richard Post author

            Sorry, I assumed you understood python. I will later send you modified code for single region.

  7. Daniel Dueñas

    I already made that only check the region that i have, but the error still showing
    What permission my account need? because I have Administrator privileges so i don’t know why is the error

    Reply
    1. richard Post author

      I see you are using the AutoScaleCompute script, can you try the AutoScaleAll script, this script does not use list request for most resources, but the search feature.

      Reply
      1. Daniel Dueñas

        I still have the error, i really dont know whats wrong

        2019-09-30 16:34:56,389 Day of week: Monday – Current hour: 16
        2019-09-30 16:34:56,405 2019-09-30 21:34:56.405503: Request: POST https://query
        .us-ashburn-1.oraclecloud.com/20180409/resources
        2019-09-30 16:34:56,826 2019-09-30 21:34:56.826704: Request: GET https://identi
        ty.us-ashburn-1.oraclecloud.com/20160918/compartments/
        2019-09-30 16:34:56,967 2019-09-30 21:34:56.967104: Request: GET https://identi
        ty.us-ashburn-1.oraclecloud.com/20160918/compartments/
        2019-09-30 16:34:57,169 2019-09-30 21:34:57.169905: Request: GET https://iaas.u
        s-ashburn-1.oraclecloud.com/20160918/instancePools
        Traceback (most recent call last):
        File “AutoScaleAll.py”, line 124, in
        pools = pool.list_instance_pools(compartment_id=c.id).data
        File “C:\Python\Python37-32\lib\site-packages\oci\core\compute_management_clie
        nt.py”, line 1885, in list_instance_pools
        response_type=”list[InstancePoolSummary]”)
        File “C:\Python\Python37-32\lib\site-packages\oci\base_client.py”, line 229, i
        n call_api
        response = self.request(request)
        File “C:\Python\Python37-32\lib\site-packages\oci\base_client.py”, line 352, i
        n request
        self.raise_service_error(request, response)
        File “C:\Python\Python37-32\lib\site-packages\oci\base_client.py”, line 522, i
        n raise_service_error
        original_request=request)
        oci.exceptions.ServiceError: {‘opc-request-id’: ‘XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
        XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX’, ‘code’: ‘N
        otAuthorizedOrNotFound’, ‘message’: ‘Authorization failed or requested resource
        not found.’, ‘status’: 404}

        Reply
        1. richard Post author

          Not everything is covered by the Search API call, in this case it is saying you can not list Instance Pools. I really think this is a policy issue on your side.

          Do you have admin rights in the whole account, including root compartment, or just have admin rights on a compartment?

          Reply
  8. ibra

    Hello Richard,

    First thanks alot for your great work.

    We are planning to save money by shutting down the compute instances only during weekends.
    i have two questions :
    Oracle: (“Stopping an instance using the instance’s OS does not stop billing for that instance. If you stop an instance this way, be sure to also stop it from the Console or API.”)

    1- your script is using the API calls right ?
    2- your code including both ( auto scale up/down & start/stop ) but we need to use only the start/stop instances is there any concerns by running the whole code or you can share the compute instance part only.

    Thank again.
    Regards
    Ibra

    Reply
    1. richard Post author

      Hi,
      Yes the script uses the official OCI APIs, so when it turns of an instance, it actually stops the billing for it.
      For compute instances you can use the 0 value for turning off and 1 for turning on.

      In the script you can also configure how you want to shutdown the instance; hard stop or soft stop.

      Reply
  9. pete

    Hello Richard,
    Nice work !

    I only have rights in the specific compartment hence i have authorization problem.

    python3 CreateNameSpaces.py
    /usr/lib/python3.6/site-packages/oci/packages/cryptography/hazmat/bindings/openssl/binding.py:179: CryptographyDeprecationWarning: OpenSSL version 1.0.2 is no longer supported by the OpenSSL project, please upgrade. The next version of cryptography will completely remove support for it.
    utils.CryptographyDeprecationWarning,
    Traceback (most recent call last):
    File “CreateNameSpaces.py”, line 41, in
    compartment = identity.get_compartment(compartment_id=SearchCompID).data
    File “/usr/lib/python3.6/site-packages/oci/identity/identity_client.py”, line 4321, in get_compartment
    response_type=”Compartment”)
    File “/usr/lib/python3.6/site-packages/oci/base_client.py”, line 263, in call_api
    return self.request(request)
    File “/usr/lib/python3.6/site-packages/oci/base_client.py”, line 384, in request
    self.raise_service_error(request, response)
    File “/usr/lib/python3.6/site-packages/oci/base_client.py”, line 554, in raise_service_error
    original_request=request)
    oci.exceptions.ServiceError: {‘opc-request-id’: ’61C6B65047FD488D90947C2078F2E9F1/67088E35729DDF2A02F850B1EB7DC25A/7090E69470055A284ADED3531975B2A6′, ‘code’: ‘NotAuthorizedOrNotFound’, ‘message’: ‘Authorization failed or requested resource not found’, ‘status’: 404}

    Would you be able to post the code for specific compartment access scenario ?

    Reply
    1. richard Post author

      Can you create the namespaces manually using the OCI Web Console?

      Reply
  10. Mario

    Richard, how you deal with the service limit for compute simultaneous startup? Thanks.

    Reply
    1. richard Post author

      The script re-tries call when the API limit is reached.

      Reply
  11. Mike Turner

    Richard –
    Nice work I was up and running quickly with your instructions.
    I would like to try to use the notifications to push email, I created the Topic in OCI and put the Topic OCI ID in the scrtipt, but it does not send nay notifications.

    Is there something else I have to do? I assumed it used the notifications service so I did not create an email domain or anything in OCI. I just subscribed and verified by subscription in the notification service.

    Reply
  12. MS Mahmood

    Hi Richard,
    Can you please share the names of REST APIs Which are related to start and stop of an instance. Also I need to know the name of REST API which can be used to change the shape of instance from small to large or large to small actually want to resize it. Thanks

    Reply
  13. Marvin

    Hi Richard,

    Great solution. I have deployed this on a group of our test servers but have noticed some strange behaviour
    We are using EDT timezone with the following tags

    Schedule.Friday: 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0
    Schedule.Saturday: 0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1

    However, the instances are rebooting at midnight Thursday/Friday

    I have tested and change the tag to below but the error persists.
    Schedule.Friday: *,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0

    Any possible ideas of what might be causing this?

    Reply
  14. liz hall

    Richard,

    we have been using your scripts for a year. Saving us lots of money! We tried this, but found that there was a quick on/off for 2 hours. We don’t see what is incorrect.

    VM turned off Sunday for 2 hours. Then back on Sunday hrough Friday 0400. Shut down at Friday for 3 minutes, then backup till the expected Friday shutdown.

    Our VM is Eastern timezone.

    WeekDay
    1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
    Friday
    1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0
    Sunday
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1
    Weekend
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0

    Reply
  15. alessandro

    HI,
    one question :

    is there a version on windows VM?

    thanks for support

    Reply
  16. Trevor North

    Would this also work for stopping compute nodes of ExaCC ?

    And a more general question – does OCI provide a way to run these scripts without needing to access a command shell i.e. is there some scheduling tool built into OCI (or maybe a template for one using e.g. OCI functions) that can make API calls at set times ?

    I guess we can setup a linux VM that has the cli tools installed and can be run from an enterprise scheduler to run such API calls but would be nice if there was an existing native OCI solution for this since it should be a common thing e.g. shutting down VM instances that are tagged such as non prod.

    Thanks in advance

    Reply

Leave a Reply to Manu Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.