Skip to content
Browse files

add initial ec2 commands

  • Loading branch information...
0 parents commit 24cad0b232dfd0dd14bbdb2749f45502f6bddcb3 @armstrtw committed Oct 5, 2011
Showing with 141 additions and 0 deletions.
  1. +6 −0 .gitignore
  2. +9 −0 DESCRIPTION
  3. +13 −0 NAMESPACE
  4. +112 −0 R/ec2.controler.R
  5. +1 −0 tests/test.startcluster.R
6 .gitignore
@@ -0,0 +1,6 @@
+\#*\#
+*~
+*.d
+*.rc
+*.o
+*.so
9 DESCRIPTION
@@ -0,0 +1,9 @@
+Package: aws.tools
+Title: R package to use ec2 and s3 commands
+Version: 0.0.1
+Maintainer: Whit Armstrong <armstrong.whit@gmail.com>
+Author: Whit Armstrong <armstrong.whit@gmail.com>
+Description: R package for Amazon EC2/S3
+License: GPL-3
+URL: http://github.com/armstrtw/aws.tools
+BugReports: http://github.com/armstrtw/aws.tools/issues
13 NAMESPACE
@@ -0,0 +1,13 @@
+export(
+ instances.from.reservation,
+ pending.instance,
+ sleep.while.pending,
+ startCluster,
+ get.master,
+ instances.to.dataframe,
+ get.instances.from.cluster,
+ stopCluster,
+ ec2din,
+ ec2stop.instance,
+ ec2stop.reservation
+ )
112 R/ec2.controler.R
@@ -0,0 +1,112 @@
+###########################################################################
+## Copyright (C) 2011 Whit Armstrong ##
+## ##
+## This program is free software: you can redistribute it and#or modify ##
+## it under the terms of the GNU General Public License as published by ##
+## the Free Software Foundation, either version 3 of the License, or ##
+## (at your option) any later version. ##
+## ##
+## This program is distributed in the hope that it will be useful, ##
+## but WITHOUT ANY WARRANTY; without even the implied warranty of ##
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ##
+## GNU General Public License for more details. ##
+## ##
+## You should have received a copy of the GNU General Public License ##
+## along with this program. If not, see <http:##www.gnu.org#licenses#>. ##
+###########################################################################
+
+instances.from.reservation <- function(reservation.id,verbose=TRUE) {
+ ec2din(paste("ec2din","reservation-id=",reservation.id,sep=""),verbose=verbose)
+
+ res <- system(cmd,intern=TRUE)
+ instances <- res[grep("^INSTANCE",res),]
+ instances.to.dataframe(instances)
+}
+
+pending.instance <- function(reservation.id) {
+ instances <- instances.from.reservation(reservation.id)
+
+ ## test both state and public dns status
+ any(instances[,"InstanceState"]=="pending" || instances[,"PublicDNS"] == "(nil)")
+}
+
+sleep.while.pending <- function(reservation.id,sleep.time=1,verbose=TRUE) {
+ while(pending.instance(reservation.id)) {
+ if(verbose) { cat(".") }
+ Sys.sleep(sleep.time)
+ }
+ if(verbose) { cat("\n") }
+}
+
+startCluster <- function(ami,key,instance.count,instance.type,verbose=TRUE) {
+ cmd <- paste("ec2-run-instances",
+ ami,
+ "--show-empty-fields",
+ "--key",key,
+ "--instance-count",instance.count,
+ "--instance-type",instance.type)
+
+ if(verbose) {
+ cat("using this cmd:\n")
+ print(cmd)
+ }
+ res <- system(cmd,intern=TRUE)
+ reservation <- strsplit(res[[1]],split="\t")[[1]][-1]
+ sleep.while.pending(reservation[1])
+ instances <- instances.from.reservation(reservation[1])
+ ans <- list(reservation=reservation,instances=instances)
+ class(ans) <- "ec2.cluster"
+ ans
+}
+
+get.master <- function(cluster) {
+ if(class(cluster) != "ec2.cluster") {
+ stop("need class of type: ec2.cluster.")
+ }
+
+ cluster[which.min(as.integer(cluster[,"AMILaunchIndex"])),]
+}
+
+instances.to.dataframe <- function(x) {
+ instances <- do.call(rbind,strsplit(x,split="\t"))
+ colnames(instances) <- c("TypeIdentifier","InstanceID","AmiID","PublicDNS","PrivateDNS","InstanceState","KeyName","AMILaunchIndex","ProductCodes","InstanceType","InstanceLaunchTime","AvailabilityZone","KernelID","RAMDiskID","MonitoringState","PublicIPAddress","PrivateIPAddress","Tenancy","SubnetID","VpcID","TypeOfRootDevice","PlacementGroup","VirtualizationType","IDsOfEachSecurityGroups","Tags","HypervisorType","BlockdeviceIdentifier")
+ instances
+}
+
+get.instances.from.cluster <- function(cluster) {
+ cluster[["instances"]][,"InstanceID"]
+}
+
+stopCluster <- function(cluster) {
+ ans <- list()
+ for(instance in get.instances.from.cluster(cluster)) {
+ ans[[instance]] <- ec2stop(instance)
+ }
+ do.call(rbind,ans)
+}
+
+ec2din <- function(filters=NULL,verbose=TRUE) {
+ aws.cmd <- paste("ec2-describe-instances",
+ paste("--filter",filters))
+ if(verbose) {
+ cat("ec2din, using this cmd:\n")
+ print(aws.cmd)
+ }
+
+ system(aws.cmd,inern=TRUE)
+}
+
+ec2stop.instance <- function(instance.id) {
+ cmd <- paste("ec2-stop-instances",instance.id)
+ res <- system(cmd,intern=TRUE)
+ res
+}
+
+ec2stop.reservation <- function(reservation.id) {
+ instances <- instances.from.reservation(reservation.id)
+ ans <- list()
+ for(inst in instances[,"InstanceID"]) {
+ ans[[inst]] <- ec2stop.instance(inst)
+ }
+ do.call(rbind,ans)
+}
1 tests/test.startcluster.R
@@ -0,0 +1 @@
+cl <- startCluster(ami="ami-a531fccc",key="kls-ec2",instance.count=1,instance.type="t1.micro",verbose=TRUE)

0 comments on commit 24cad0b

Please sign in to comment.
Something went wrong with that request. Please try again.