github.com/coreos/mantle@v0.13.0/platform/machine/azure/flight.go (about)

     1  // Copyright 2018 Red Hat
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package azure
    16  
    17  import (
    18  	"fmt"
    19  
    20  	ctplatform "github.com/coreos/container-linux-config-transpiler/config/platform"
    21  	"github.com/coreos/pkg/capnslog"
    22  
    23  	"github.com/coreos/mantle/platform"
    24  	"github.com/coreos/mantle/platform/api/azure"
    25  )
    26  
    27  const (
    28  	Platform platform.Name = "azure"
    29  )
    30  
    31  var (
    32  	plog = capnslog.NewPackageLogger("github.com/coreos/mantle", "platform/machine/azure")
    33  )
    34  
    35  type flight struct {
    36  	*platform.BaseFlight
    37  	api        *azure.API
    38  	SSHKey     string
    39  	FakeSSHKey string
    40  }
    41  
    42  // NewFlight creates an instance of a Flight suitable for spawning
    43  // instances on the Azure platform.
    44  func NewFlight(opts *azure.Options) (platform.Flight, error) {
    45  	api, err := azure.New(opts)
    46  	if err != nil {
    47  		return nil, err
    48  	}
    49  
    50  	if err = api.SetupClients(); err != nil {
    51  		return nil, fmt.Errorf("setting up clients: %v", err)
    52  	}
    53  
    54  	bf, err := platform.NewBaseFlight(opts.Options, Platform, ctplatform.Azure)
    55  	if err != nil {
    56  		return nil, err
    57  	}
    58  
    59  	af := &flight{
    60  		BaseFlight: bf,
    61  		api:        api,
    62  	}
    63  
    64  	keys, err := af.Keys()
    65  	if err != nil {
    66  		af.Destroy()
    67  		return nil, err
    68  	}
    69  	af.SSHKey = keys[0].String()
    70  	af.FakeSSHKey, err = platform.GenerateFakeKey()
    71  	if err != nil {
    72  		return nil, err
    73  	}
    74  
    75  	return af, nil
    76  }
    77  
    78  // NewCluster creates an instance of a Cluster suitable for spawning
    79  // instances on the Azure platform.
    80  func (af *flight) NewCluster(rconf *platform.RuntimeConfig) (platform.Cluster, error) {
    81  	bc, err := platform.NewBaseCluster(af.BaseFlight, rconf)
    82  	if err != nil {
    83  		return nil, err
    84  	}
    85  
    86  	ac := &cluster{
    87  		BaseCluster: bc,
    88  		flight:      af,
    89  	}
    90  
    91  	if !rconf.NoSSHKeyInMetadata {
    92  		ac.sshKey = af.SSHKey
    93  	} else {
    94  		ac.sshKey = af.FakeSSHKey
    95  	}
    96  
    97  	ac.ResourceGroup, err = af.api.CreateResourceGroup("kola-cluster")
    98  	if err != nil {
    99  		return nil, err
   100  	}
   101  
   102  	ac.StorageAccount, err = af.api.CreateStorageAccount(ac.ResourceGroup)
   103  	if err != nil {
   104  		return nil, err
   105  	}
   106  
   107  	_, err = af.api.PrepareNetworkResources(ac.ResourceGroup)
   108  	if err != nil {
   109  		return nil, err
   110  	}
   111  
   112  	af.AddCluster(ac)
   113  
   114  	return ac, nil
   115  }
   116  
   117  func (af *flight) Destroy() {
   118  	af.BaseFlight.Destroy()
   119  }