Skip to content

Commit

Permalink
VMWare integration application files.
Browse files Browse the repository at this point in the history
  • Loading branch information
Apurva Singh committed Mar 10, 2014
0 parents commit 0b48569
Show file tree
Hide file tree
Showing 16 changed files with 951 additions and 0 deletions.
Binary file added VMwareIntegration-Installinstructions.docx
Binary file not shown.
Binary file added bin/vmhalo
Binary file not shown.
Binary file added halo_installers/cphalo-2.8.2-win64.exe
Binary file not shown.
39 changes: 39 additions & 0 deletions halo_installers/halo_deb_install.sh
@@ -0,0 +1,39 @@
#!/bin/bash

# CloudPassage Halo Daemon
# Unattended installation script for Vendor Integrations
# -------------------------------------------------------------------------
# This script is intended to be used for an unattended installation
# of the CloudPassage Halo daemon.
#
# IMPORTANT NOTES
#
# * This script may require adjustment to conform to your server's
# configuration. Please review this script and test it on a server
# before using it to install the Halo daemon on multiple servers.
#
# * This script contains the CloudPassage Halo Daemon Registration Key owned by
# Vendor Integrations. Keep this script safe - handle it as
# you would the password to your CloudPassage portal account!
#

# add CloudPassage repository
echo 'deb http://packages.cloudpassage.com/debian debian main' | sudo tee /etc/apt/sources.list.d/cloudpassage.list > /dev/null

# install curl
sudo apt-get -y install curl

# import CloudPassage public key
curl http://packages.cloudpassage.com/cloudpassage.packages.key | sudo apt-key add -

# update apt repositories
sudo apt-get update > /dev/null

# install the daemon
sudo apt-get -y install cphalo

echo "Key=$1" ; echo "Tag=$2"

# start the daemon for the first time
sudo /etc/init.d/cphalod start --daemon-key=$1 --tag=$2

35 changes: 35 additions & 0 deletions halo_installers/halo_rh_install.sh
@@ -0,0 +1,35 @@
#!/bin/bash

# CloudPassage Halo Daemon
# Unattended installation script for Vendor Integrations
# -------------------------------------------------------------------------
# This script is intended to be used for an unattended installation
# of the CloudPassage Halo daemon.
#
# IMPORTANT NOTES
#
# * This script may require adjustment to conform to your server's
# configuration. Please review this script and test it on a server
# before using it to install the Halo daemon on multiple servers.
#
# * This script contains the CloudPassage Halo Daemon Registration Key owned by
# Vendor Integrations. Keep this script safe - handle it as
# you would the password to your CloudPassage portal account!
#

# add CloudPassage repository
echo -e '[cloudpassage]\nname=CloudPassage\nbaseurl=http://packages.cloudpassage.com/redhat/$basearch\ngpgcheck=1' | tee /etc/yum.repos.d/cloudpassage.repo > /dev/null

# import CloudPassage public key
rpm --import http://packages.cloudpassage.com/cloudpassage.packages.key

# update yum repositories
yum check-update > /dev/null

# install the daemon
yum -y install cphalo

echo "Key=$1" ; echo "Tag=$2"

# start the daemon for the first time
/etc/init.d/cphalod start --daemon-key=$1 --tag=$2
20 changes: 20 additions & 0 deletions src/Makefile
@@ -0,0 +1,20 @@
#
# Makefile for vmhalo program
#
OBJS=vmhalo.o vixconnection.o vmhconfig.o

vmhalo: $(OBJS)
g++ -g -o vmhalo $(OBJS) -lvixAllProducts -ldl -lpthread
cp vmhalo ../bin

vmhalo.o: vmhalo.cpp vixconnection.hpp vmhconfig.hpp
g++ -g -c -I/usr/include/vmware-vix/ vmhalo.cpp

vmhconfig.o: vmhconfig.cpp vmhconfig.hpp
g++ -g -c -I/usr/include/vmware-vix/ vmhconfig.cpp

vixconnection.o: vixconnection.cpp vixconnection.hpp
g++ -g -c -I/usr/include/vmware-vix/ vixconnection.cpp

clean:
rm -f vmhalo $(OBJS)
227 changes: 227 additions & 0 deletions src/vixconnection.cpp
@@ -0,0 +1,227 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef WIN32
#define strcasecmp stricmp
#endif

#include <vix.h>

#include "vixconnection.hpp"
#include "vmhconfig.hpp"

VixConnection::VixConnection()
{
strcpy(hostname,"");
port = 0;
strcpy(username,"");
strcpy(password,"");
hostHandle = VIX_INVALID_HANDLE;
retryPasswordAllowed = true;
}

bool VixConnection::connect()
{
VixError err;
VixHandle jobHandle = VixHost_Connect(VIX_API_VERSION, serverType, hostname, port,
username, password, 0,
VIX_INVALID_HANDLE, NULL, NULL);
err = VixJob_Wait(jobHandle, VIX_PROPERTY_JOB_RESULT_HANDLE, &hostHandle, VIX_PROPERTY_NONE);
Vix_ReleaseHandle(jobHandle);
if (VIX_FAILED(err)) {
fprintf(stderr, "Connect failure: %s\n", Vix_GetErrorText(err, NULL));
return false;
} else
return true;
}

void VixConnection::setHostInfo(char *hostname, int port)
{
strncpy(this->hostname,hostname,256);
this->port = port;
}

void VixConnection::setUserInfo(char *username, char *password)
{
strncpy(this->username,username,32);
strncpy(this->password,password,32);
}

typedef struct s_server_type {
int typeNum;
const char *typeName;
} ServerType;

static ServerType type_list[] = {
{ VIX_SERVICEPROVIDER_VMWARE_SERVER, "server" },
{ VIX_SERVICEPROVIDER_VMWARE_WORKSTATION, "workstation" },
{ VIX_SERVICEPROVIDER_VMWARE_PLAYER, "player" },
{ VIX_SERVICEPROVIDER_VMWARE_VI_SERVER, "vi" },
{ VIX_SERVICEPROVIDER_VMWARE_WORKSTATION_SHARED, "shared" }
};
static int type_count = sizeof(type_list)/sizeof(type_list[0]);

void VixConnection::setServerType(const char *type)
{
for (int i = 0; i < type_count; i++) {
if (strcasecmp(type_list[i].typeName,type) == 0) {
serverType = type_list[i].typeNum;
return;
}
}
fprintf(stderr,"Unknown vm host type: %s\n",type);
}

typedef struct s_callback_info {
FinderCallback userCB;
VixConnection *vconn;
VmhConfig *config;
} CallbackInfo;
static CallbackInfo cbinfo = { NULL, NULL, NULL };

static void internalFinderCallback(VixHandle job, VixEventType event, VixHandle moreInfo, void *cd)
{
char *location = NULL;
if (event != VIX_EVENTTYPE_FIND_ITEM)
return;

VixError err = Vix_GetProperties(moreInfo, VIX_PROPERTY_FOUND_ITEM_LOCATION, &location, VIX_PROPERTY_NONE);
if (VIX_SUCCEEDED(err)) {
if (cbinfo.userCB != NULL) {
(*cbinfo.userCB)(location);
} else {
fprintf(stderr,"Found VM: %s\n",location);
}
} else {
fprintf(stderr,"GetProperties failed: %s",Vix_GetErrorText(err,NULL));
}
}

void VixConnection::findVMs(FinderCallback cb)
{
cbinfo.userCB = cb; // store info for later, should pass as "Callback Data" (cd) argument
cbinfo.vconn = this;
VixHandle jobHandle = VixHost_FindItems(hostHandle,VIX_FIND_RUNNING_VMS,VIX_INVALID_HANDLE,
-1,internalFinderCallback,NULL);
VixError err = VixJob_Wait(jobHandle, VIX_PROPERTY_NONE);
Vix_ReleaseHandle(jobHandle);
if (VIX_FAILED(err)) {
fprintf(stderr,"Find VMs failed: %s\n",Vix_GetErrorText(err,NULL));
return;
}
}

void VixConnection::disconnect()
{
VixHost_Disconnect(hostHandle);
}

bool VixConnection::runCmd(char *cmd, char *args)
{
VixHandle jobHandle = VixVM_RunProgramInGuest(vmHandle,cmd,args,0,VIX_INVALID_HANDLE,NULL,NULL);
VixError err = VixJob_Wait(jobHandle, VIX_PROPERTY_NONE);
Vix_ReleaseHandle(jobHandle);
if (VIX_FAILED(err)) {
fprintf(stderr,"Failed to run program %s: %s\n",cmd,Vix_GetErrorText(err,NULL));
return false;
}
return true;
}

bool VixConnection::runScript(char *shell, char *scriptText)
{
VixHandle jobHandle = VixVM_RunScriptInGuest(vmHandle,shell,scriptText,
0,VIX_INVALID_HANDLE,NULL,NULL);
VixError err = VixJob_Wait(jobHandle, VIX_PROPERTY_NONE);
Vix_ReleaseHandle(jobHandle);
if (VIX_FAILED(err)) {
fprintf(stderr,"Login to VM failed: %s\n",Vix_GetErrorText(err,NULL));
return false;
}
return true;
}

bool VixConnection::openVM(char *vmxpath, char *guestUsername, char *guestPassword)
{
char username_buf[4096];
char password_buf[4096];
VixHandle jobHandle = VixVM_Open(hostHandle,vmxpath,NULL,NULL);
VixError err = VixJob_Wait(jobHandle, VIX_PROPERTY_JOB_RESULT_HANDLE, &vmHandle, VIX_PROPERTY_NONE);
Vix_ReleaseHandle(jobHandle);
if (VIX_FAILED(err)) {
fprintf(stderr,"Open VM failed: %s\n",Vix_GetErrorText(err,NULL));
return false;
} else {
jobHandle = VixVM_LoginInGuest(vmHandle, guestUsername, guestPassword, 0, NULL, NULL);
err = VixJob_Wait(jobHandle, VIX_PROPERTY_NONE);
Vix_ReleaseHandle(jobHandle);
if (VIX_FAILED(err)) {
fprintf(stderr,"Login to VM failed: %s\n",Vix_GetErrorText(err,NULL));
if (retryPasswordAllowed) {
printf("To retry login with different credentials, enter new username: ");
fflush(stdout);
fgets(username_buf,4000,stdin);
trim(username_buf);
if (strlen(username_buf) > 0) {
printf("Enter new password: ");
fflush(stdout);
fgets(password_buf,4000,stdin);
trim(password_buf);
jobHandle = VixVM_LoginInGuest(vmHandle, username_buf, password_buf, 0, NULL, NULL);
err = VixJob_Wait(jobHandle, VIX_PROPERTY_NONE);
Vix_ReleaseHandle(jobHandle);
if (VIX_FAILED(err)) {
fprintf(stderr,"Login to VM failed again: %s\n",Vix_GetErrorText(err,NULL));
return false;
}
return true; // retry succeeded
}
}
return false;
}
return true;
}
}

bool VixConnection::copyFromVM(char *remotePath, char *localPath)
{
VixHandle jobHandle = VixVM_CopyFileFromGuestToHost(vmHandle, remotePath, localPath,
0, VIX_INVALID_HANDLE, NULL, NULL);
VixError err = VixJob_Wait(jobHandle, VIX_PROPERTY_NONE);
Vix_ReleaseHandle(jobHandle);
if (VIX_FAILED(err)) {
fprintf(stderr,"File copy from VM failed: %s: %s\n",remotePath,Vix_GetErrorText(err,NULL));
return false;
}
return true;
}

bool VixConnection::copyToVM(char *localPath, char *remotePath)
{
VixHandle jobHandle = VixVM_CopyFileFromHostToGuest(vmHandle, localPath, remotePath,
0, VIX_INVALID_HANDLE, NULL, NULL);
VixError err = VixJob_Wait(jobHandle, VIX_PROPERTY_NONE);
Vix_ReleaseHandle(jobHandle);
if (VIX_FAILED(err)) {
fprintf(stderr,"File copy to VM failed: %s: %s\n",localPath,Vix_GetErrorText(err,NULL));
return false;
}
return true;
}

bool VixConnection::fileExistsInVM(char *remotePath)
{
int fileExists = 0;
VixHandle jobHandle = VixVM_FileExistsInGuest(vmHandle, remotePath, NULL, NULL);
VixError err = VixJob_Wait(jobHandle, VIX_PROPERTY_NONE);
if (VIX_FAILED(err)) {
Vix_ReleaseHandle(jobHandle);
return false;
}
err = Vix_GetProperties(jobHandle,VIX_PROPERTY_JOB_RESULT_GUEST_OBJECT_EXISTS,&fileExists,VIX_PROPERTY_NONE);
Vix_ReleaseHandle(jobHandle);
if (VIX_FAILED(err)) {
return false;
}
return fileExists != 0;
}
37 changes: 37 additions & 0 deletions src/vixconnection.hpp
@@ -0,0 +1,37 @@
#ifndef VIX_CONNECTION_HPP
#define VIX_CONNECTION_HPP

class VixConnection;
class VmhConfig;

typedef void (*FinderCallback)(char *vmxPath);

class VixConnection
{
public:
VixConnection();
bool connect();
void setHostInfo(char *hostname, int port = 0);
void setUserInfo(char *username, char *password);
void setServerType(int type) { serverType = type; }
void setServerType(const char *type);
void disconnect();
void findVMs(FinderCallback cb);
bool openVM(char *vmxpath, char *guestUsername, char *guestPassword);
bool runCmd(char *cmd, char *args);
bool runScript(char *shell, char *scriptText);
bool copyFromVM(char *remotePath, char *localPath);
bool copyToVM(char *localPath, char *remotePath);
bool fileExistsInVM(char *remotePath);
private:
char hostname[256];
int port;
char username[32];
char password[32];
int serverType;
VixHandle hostHandle;
VixHandle vmHandle;
bool retryPasswordAllowed;
};

#endif
Binary file added src/vixconnection.o
Binary file not shown.
Binary file added src/vmhalo
Binary file not shown.

0 comments on commit 0b48569

Please sign in to comment.