2016-09-17 6 views
0

私はEC2インスタンスを操作するためにjcloudsを使い始めています。jcloudsで既存のsecurityGroupとpemファイルを使用する - Java

private ComputeService compute; 

public void add(Integer instances) { 
     try { 
      logger.info("------------------------------------------------------"); 
      logger.info(String.format(">> adding node to group %s%n", this.groupname)); 
      // Default template chooses the smallest size on an operating system 
      // that tested to work with java, which tends to be Ubuntu or CentOS 
      TemplateBuilder templateBuilder = this.compute.templateBuilder(); 
      // note this will create a user with the same name as you on the 
      // node. ex. you can connect via ssh publicip 
      Statement bootInstructions = AdminAccess.standard(); 

      // to run commands as root, we use the runScript option in the template. 
      templateBuilder.options(runScript(bootInstructions)); 

      Template template = templateBuilder.build(); 
      // add a custom security group 

      NodeMetadata node = getOnlyElement(this.compute.createNodesInGroup(this.groupname, instances, template)); 
      logger.info(String.format("<< node %s: %s%n", node.getId(), 
         concat(node.getPrivateAddresses(), node.getPublicAddresses()))); 
      logger.info("------------------------------------------------------"); 
     } catch (Exception e) { 
      logger.error(e.getMessage()); 
      logger.info("------------------------------------------------------"); 
     } 
    } 

認証が正しいと仮定して groupname = defaultてください:したがって、私は次のような方法があります。私は1つのインスタンスが作成されていても

this.compute.add(1); 

を実行すると、jcloudsは新しいsecurity groupkey pairたびに作成されます。私は既存のfoo.pemファイルとdefaultセキュリティグループを持っています。たとえば、 security group = jclouds#defaultです。既存のセキュリティグループとキーバリューをどのように活用できますか?

答えて

0

既存のセキュリティグループまたはキーペアを使用する場合は、テンプレートビルダーにそれらを提供することにより、それを行うことができます。

templateBuilder.securityGroups(<existing security groups>); 
templateBuilder.as(AWSEC2TemplateOptions.class).keyPair(<existing key pair>); 
templateBuilder.options(runScript(bootInstructions)); 

keypairメソッドを使用するために、あなたはオプションをキャストする必要があることそのオプションはポータブルインターフェイスでは利用できないため、AWSクラスに追加する必要があります。

鍵ベースのアクセスを使用して(スクリプトを実行するために)SSH経由でノードにアクセスする場合、jcloudsはノードに「秘密鍵」を提示する必要があります。次の2つのオプションがあります。事前にssh-agentに鍵をロードするか、秘密鍵を提供するためにtemplateOptions.overrideLoginPrivateKeyを使用して、jcloudsがノードに正しくログインできるようにします。

関連する問題