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

25 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

Leave a 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.