########
# Copyright (c) 2014 GigaSpaces Technologies Ltd. All rights reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#        http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
#    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#    * See the License for the specific language governing permissions and
#    * limitations under the License.

from cloudify_libcloud import (CosmoOnLibcloudDriver,
                               LibcloudKeypairController,
                               LibcloudSGController,
                               LibcloudFloatingIpController,
                               LibcloudServerController,
                               LibcloudValidator)
from libcloud.compute.types import NodeState

from os.path import expanduser
import os
import time
from fabric.api import put, env
from libcloud.compute.types import KeyPairDoesNotExistError
import tempfile
import shutil
from fabric.context_managers import settings
import errno
import json


CREATE_IF_MISSING = 'create_if_missing'
TIMEOUT = 3000


# declare which ports should be opened during provisioning
EXTERNAL_MGMT_PORTS = (22, 8100, 80)  # SSH, REST service (TEMP), REST and UI
INTERNAL_MGMT_PORTS = (5555, 5672, 53229, 8101) # Riemann, RabbitMQ, FileServer, Internal REST  # NOQA
INTERNAL_AGENT_PORTS = (22,)


class EC2CosmoOnLibcloudDriver(CosmoOnLibcloudDriver):

    def __init__(self, provider_config, provider_context, connector):
        super(EC2CosmoOnLibcloudDriver, self)\
            .__init__(provider_config, provider_context)
        self.keypair_controller = EC2LibcloudKeypairController(connector)