Skip to content

Commit

Permalink
implement vagrant global-status machine detection
Browse files Browse the repository at this point in the history
  • Loading branch information
chriswayoub committed Nov 9, 2014
1 parent ed684e6 commit e580f27
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 13 deletions.
6 changes: 6 additions & 0 deletions Vagrant Manager.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
8308E133188662BD001E0B45 /* AboutWindow.xib in Resources */ = {isa = PBXBuildFile; fileRef = 8308E131188662BD001E0B45 /* AboutWindow.xib */; };
8308E135188668A1001E0B45 /* WebKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8308E134188668A1001E0B45 /* WebKit.framework */; };
8308E14018869FAC001E0B45 /* Bookmark.m in Sources */ = {isa = PBXBuildFile; fileRef = 8308E13F18869FAC001E0B45 /* Bookmark.m */; };
830BAC351A0F278D00AE5F9C /* VagrantGlobalStatusScanner.m in Sources */ = {isa = PBXBuildFile; fileRef = 830BAC341A0F278D00AE5F9C /* VagrantGlobalStatusScanner.m */; };
83103A571982180C00B340BB /* ParallelsServiceProvider.m in Sources */ = {isa = PBXBuildFile; fileRef = 83103A561982180C00B340BB /* ParallelsServiceProvider.m */; };
83103A5A19821A9E00B340BB /* ParallelsMachineInfo.m in Sources */ = {isa = PBXBuildFile; fileRef = 83103A5919821A9E00B340BB /* ParallelsMachineInfo.m */; };
8339DCA1187E756B0036E162 /* TaskOutputWindow.m in Sources */ = {isa = PBXBuildFile; fileRef = 8339DC9F187E756B0036E162 /* TaskOutputWindow.m */; };
Expand Down Expand Up @@ -142,6 +143,8 @@
8308E134188668A1001E0B45 /* WebKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = WebKit.framework; path = System/Library/Frameworks/WebKit.framework; sourceTree = SDKROOT; };
8308E13E18869FAC001E0B45 /* Bookmark.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Bookmark.h; sourceTree = "<group>"; };
8308E13F18869FAC001E0B45 /* Bookmark.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Bookmark.m; sourceTree = "<group>"; };
830BAC331A0F278D00AE5F9C /* VagrantGlobalStatusScanner.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = VagrantGlobalStatusScanner.h; sourceTree = "<group>"; };
830BAC341A0F278D00AE5F9C /* VagrantGlobalStatusScanner.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = VagrantGlobalStatusScanner.m; sourceTree = "<group>"; };
83103A551982180C00B340BB /* ParallelsServiceProvider.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ParallelsServiceProvider.h; sourceTree = "<group>"; };
83103A561982180C00B340BB /* ParallelsServiceProvider.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ParallelsServiceProvider.m; sourceTree = "<group>"; };
83103A5819821A9E00B340BB /* ParallelsMachineInfo.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ParallelsMachineInfo.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -401,6 +404,8 @@
83E7CAB31928C2E9001C86F6 /* VirtualMachineInfo.m */,
83FD354319826DDC002EEA2D /* NFSScanner.h */,
83FD354419826DDC002EEA2D /* NFSScanner.m */,
830BAC331A0F278D00AE5F9C /* VagrantGlobalStatusScanner.h */,
830BAC341A0F278D00AE5F9C /* VagrantGlobalStatusScanner.m */,
);
name = "Vagrant Manager";
sourceTree = "<group>";
Expand Down Expand Up @@ -732,6 +737,7 @@
8308E132188662BD001E0B45 /* AboutWindow.m in Sources */,
B1761B3D1A00C0140056582F /* NativeMenuItem.m in Sources */,
B12661181A00A650009B1635 /* NativeMenu.m in Sources */,
830BAC351A0F278D00AE5F9C /* VagrantGlobalStatusScanner.m in Sources */,
B13DC35B19232782002E5A50 /* VirtualBoxServiceProvider.m in Sources */,
B13DC36019233051002E5A50 /* VirtualBoxMachineInfo.m in Sources */,
8339DCA1187E756B0036E162 /* TaskOutputWindow.m in Sources */,
Expand Down
14 changes: 14 additions & 0 deletions Vagrant Manager/VagrantGlobalStatusScanner.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// VagrantGlobalStatusScanner.h
// Vagrant Manager
//
// Copyright (c) 2014 Lanayo. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface VagrantGlobalStatusScanner : NSObject

- (NSArray*)getInstancePaths;

@end
56 changes: 56 additions & 0 deletions Vagrant Manager/VagrantGlobalStatusScanner.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//
// VagrantGlobalStatusScanner.m
// Vagrant Manager
//
// Copyright (c) 2014 Lanayo. All rights reserved.
//

#import "VagrantGlobalStatusScanner.h"

@implementation VagrantGlobalStatusScanner

//find vagrant instances listed in the vagrant global-status command output
- (NSArray*)getInstancePaths {
NSMutableArray *paths = [[NSMutableArray alloc] init];

//get output of vagrant global-status
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath:@"/bin/bash"];
[task setArguments:@[@"-c", @"vagrant global-status"]];

NSPipe *pipe = [NSPipe pipe];
[task setStandardInput:[NSPipe pipe]];
[task setStandardOutput:pipe];

[task launch];
[task waitUntilExit];

if(task.terminationStatus == 0) {
//parse instance info from global-status output
NSData *outputData = [[pipe fileHandleForReading] readDataToEndOfFile];
NSString *outputString = [[NSString alloc] initWithData:outputData encoding:NSUTF8StringEncoding];

//search for machine state in output string
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(\\S+)\\s+(\\S+)\\s+(\\S+)\\s+(\\S+)\\s+(\\/.*)(\\n|$)" options:0 error:NULL];
NSArray *matches = [regex matchesInString:outputString options:0 range:NSMakeRange(0, [outputString length])];
for(NSTextCheckingResult *match in matches) {
//NSRange idRange = [match rangeAtIndex:1];
//NSRange nameRange = [match rangeAtIndex:2];
//NSRange providerRange = [match rangeAtIndex:3];
//NSRange stateRange = [match rangeAtIndex:4];
NSRange pathRange = [match rangeAtIndex:5];

NSString *path = [[outputString substringWithRange:pathRange] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

BOOL vagrantFileExists = [[NSFileManager defaultManager] fileExistsAtPath:[NSString pathWithComponents:@[path, @"/Vagrantfile"]]];

if(vagrantFileExists) {
[paths addObject:path];
}
}
}

return [NSArray arrayWithArray:paths];
}

@end
37 changes: 24 additions & 13 deletions Vagrant Manager/VagrantManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#import "VagrantManager.h"
#import "NFSScanner.h"
#import "VagrantGlobalStatusScanner.h"
#import "BookmarkManager.h"

@implementation VagrantManager {
Expand Down Expand Up @@ -105,9 +106,30 @@ - (void)refreshInstances {
[instances addObject:[[VagrantInstance alloc] initWithPath:bookmark.path displayName:bookmark.displayName providerIdentifier:bookmark.providerIdentifier]];
}

NSMutableArray *allPaths = [[NSMutableArray alloc] init];

//scan for NFS exports
NFSScanner *nfsScanner = [[NFSScanner alloc] init];
for(NSString *path in [nfsScanner getNFSInstancePaths]) {
//make sure it is not a bookmark and has not already been detected
if(![bookmarkManager getBookmarkWithPath:path] && ![allPaths containsObject:path]) {
[allPaths addObject:path];
[instances addObject:[[VagrantInstance alloc] initWithPath:path providerIdentifier:nil]];
}
}

//scan vagrant global-status output
VagrantGlobalStatusScanner *globalStatusScanner = [[VagrantGlobalStatusScanner alloc] init];
for(NSString *path in [globalStatusScanner getInstancePaths]) {
//make sure it is not a bookmark and has not already been detected
if(![bookmarkManager getBookmarkWithPath:path] && ![allPaths containsObject:path]) {
[allPaths addObject:path];
[instances addObject:[[VagrantInstance alloc] initWithPath:path providerIdentifier:nil]];
}
}

//create instance for each detected path
NSDictionary *detectedPaths = [self detectInstancePaths];
NSMutableArray *allPaths = [[NSMutableArray alloc] init];
for(NSString *providerIdentifier in [detectedPaths allKeys]) {
NSArray *paths = [detectedPaths objectForKey:providerIdentifier];
for(NSString *path in paths) {
Expand All @@ -118,18 +140,7 @@ - (void)refreshInstances {
}
}
}

//scan for NFS exports
NFSScanner *nfsScanner = [[NFSScanner alloc] init];
NSArray *paths = [nfsScanner getNFSInstancePaths];
for(NSString *path in paths) {
//make sure it is not a bookmark and has not already been detected
if(![bookmarkManager getBookmarkWithPath:path] && ![allPaths containsObject:path]) {
[allPaths addObject:path];
[instances addObject:[[VagrantInstance alloc] initWithPath:path providerIdentifier:nil]];
}
}


//TODO: implement "last seen" functionality. Store paths of previously seen Vagrantfiles and check if they still exist

NSMutableArray *validPaths = [[NSMutableArray alloc] init];
Expand Down

0 comments on commit e580f27

Please sign in to comment.