Skip to content

Commit

Permalink
v0.0.8
Browse files Browse the repository at this point in the history
  • Loading branch information
rimusz committed Jul 5, 2016
1 parent 53d315e commit f869146
Show file tree
Hide file tree
Showing 12 changed files with 260 additions and 3 deletions.
Binary file modified src/bin/corectl
Binary file not shown.
Binary file modified src/bin/corectld
Binary file not shown.
17 changes: 17 additions & 0 deletions src/check_blobs_version.command
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash

# get installed version
INSTALLED_VERSION=v$(/usr/local/sbin/corectld version | grep "Version:" | head -1 | awk '{print $2}' | tr -d '\r')

# get remote version
CORECTL_VERSION=$(curl -Ss https://api.github.com/repos/TheNewNormal/corectl/releases | grep "tag_name" | awk '{print $2}' | sed -e 's/"\(.*\)"./\1/' | head -1)

MATCH=$(echo "${INSTALLED_VERSION}" | grep -c "${CORECTL_VERSION}")

if [ $MATCH -eq 0 ]; then
# there is an update
echo "yes"
else
# no update available
echo "no"
fi
4 changes: 4 additions & 0 deletions src/corectl.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
134F3CEC1D2C2270002B9668 /* check_blobs_version.command in Resources */ = {isa = PBXBuildFile; fileRef = 134F3CEB1D2C2270002B9668 /* check_blobs_version.command */; };
136605381D23CCB200D497A0 /* fetch_latest_iso_alpha.command in Resources */ = {isa = PBXBuildFile; fileRef = 136605361D23CCB200D497A0 /* fetch_latest_iso_alpha.command */; };
136605391D23CCB200D497A0 /* functions.sh in Resources */ = {isa = PBXBuildFile; fileRef = 136605371D23CCB200D497A0 /* functions.sh */; };
1366053B1D23D6AD00D497A0 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1366053A1D23D6AD00D497A0 /* Foundation.framework */; };
Expand All @@ -28,6 +29,7 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
134F3CEB1D2C2270002B9668 /* check_blobs_version.command */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.sh; path = check_blobs_version.command; sourceTree = "<group>"; };
136605361D23CCB200D497A0 /* fetch_latest_iso_alpha.command */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.sh; path = fetch_latest_iso_alpha.command; sourceTree = "<group>"; };
136605371D23CCB200D497A0 /* functions.sh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.sh; path = functions.sh; sourceTree = "<group>"; };
1366053A1D23D6AD00D497A0 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
Expand Down Expand Up @@ -68,6 +70,7 @@
136605351D23CC9A00D497A0 /* scripts */ = {
isa = PBXGroup;
children = (
134F3CEB1D2C2270002B9668 /* check_blobs_version.command */,
1375976E1D2C05AA000CB88F /* start_corectld.command */,
136605361D23CCB200D497A0 /* fetch_latest_iso_alpha.command */,
136605411D23E85800D497A0 /* fetch_latest_iso_beta.command */,
Expand Down Expand Up @@ -192,6 +195,7 @@
1375975B1D2BA78E000CB88F /* bin in Resources */,
136605381D23CCB200D497A0 /* fetch_latest_iso_alpha.command in Resources */,
137597671D2BE71C000CB88F /* sudo_password.command in Resources */,
134F3CEC1D2C2270002B9668 /* check_blobs_version.command in Resources */,
136605431D23E85800D497A0 /* fetch_latest_iso_beta.command in Resources */,
1375976F1D2C05AA000CB88F /* start_corectld.command in Resources */,
);
Expand Down
Binary file not shown.
43 changes: 43 additions & 0 deletions src/corectl/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,19 @@ class AppDelegate: NSObject, NSApplicationDelegate {

// check if corectl blobs are in place
check_for_corectl_blobs()

// check for latest corectl blobs on github
check_for_corectl_blobs_github()

// enable launch at login
addToLoginItems()

// start corectld server
ServerStartShell()

// check for latest blobs on github
_ = NSTimer.scheduledTimerWithTimeInterval(3600.0, target: self, selector: #selector(AppDelegate.check_for_corectl_blobs_github), userInfo: nil, repeats: true)

// create menu programmaticly
// let Quit : NSMenuItem = NSMenuItem(title: "Quit", action: #selector(AppDelegate.Quit(_:)), keyEquivalent: "")
// statusItem.menu!.addItem(Quit)
Expand Down Expand Up @@ -114,6 +120,8 @@ class AppDelegate: NSObject, NSApplicationDelegate {
menuItem.menu?.itemWithTag(1)?.title = "Server is starting"
// start corectld server
ServerStartShell()
//
menuItem.menu?.itemWithTag(3)?.title = "Check for updates"
}


Expand Down Expand Up @@ -192,6 +200,22 @@ class AppDelegate: NSObject, NSApplicationDelegate {

// helping functions

// check for updates every hour
func check_for_corectl_blobs_github()
{
let script = NSBundle.mainBundle().resourcePath! + "/check_blobs_version.command"
let status = shell(script, arguments: [])
//
if (status == "yes"){
let menuItem : NSStatusItem = statusItem
menuItem.menu?.itemWithTag(3)?.title = "Download updates..."
let mText: String = "Corectl for macOS "
let infoText: String = "There is an update available, please run via menu Download updates... !!!"
displayWithMessage(mText, infoText: infoText)
}

}

// check sudo password
func check_sudo_password() {
let app_keychain_value = Keychain.get("coreosctl-app")
Expand Down Expand Up @@ -330,5 +354,24 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}


// shell commands to run
func shell(launchPath: String, arguments: [String]) -> String
{
let task = NSTask()
task.launchPath = launchPath
task.arguments = arguments

let pipe = NSPipe()
task.standardOutput = pipe
task.launch()

let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output = String(data: data, encoding: NSUTF8StringEncoding)!
if output.characters.count > 0 {
return output.substringToIndex(output.endIndex.advancedBy(-1))
}
return output
}

}

4 changes: 2 additions & 2 deletions src/corectl/Base.lproj/MainMenu.xib
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@
</connections>
</menuItem>
<menuItem isSeparatorItem="YES" id="7F0-f8-uD9"/>
<menuItem title="Check for updates" id="G6m-bG-AHV">
<menuItem title="Check for updates" tag="3" id="G6m-bG-AHV">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="checkForUpdates:" target="Voe-Tx-rLC" id="ZGM-0U-UMz"/>
Expand Down Expand Up @@ -715,7 +715,7 @@
</menu>
</menuItem>
<menuItem isSeparatorItem="YES" id="6MD-ne-tfW"/>
<menuItem title="Preferences" id="jv0-Md-oMt">
<menuItem title="Preferences" tag="4" id="jv0-Md-oMt">
<modifierMask key="keyEquivalentModifierMask"/>
</menuItem>
<menuItem isSeparatorItem="YES" id="gfq-fY-qL1"/>
Expand Down
187 changes: 187 additions & 0 deletions src/corectl/HelpingFunctions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
//
// HelpingFunctions.swift
// corectl
//
// Created by Rimantas Mocevicius on 05/07/2016.
// Copyright © 2016 The New Normal. All rights reserved.
//

import Foundation
import Security


// helping functions

// check for updates every hour
func check_for_corectl_blobs_github()
{
let script = NSBundle.mainBundle().resourcePath! + "/check_blobs_version.command"
let status = shell(script, arguments: [])
//
if (status == "no"){
let menuItem : NSStatusItem = statusItem
menuItem.menu?.itemWithTag(3)?.title = "Download updates..."
let mText: String = "Corectl for macOS "
let infoText: String = "There is an update available, please run via menu Download updates... !!!"
displayWithMessage(mText, infoText: infoText)
}

}

// check sudo password
func check_sudo_password() {
let app_keychain_value = Keychain.get("coreosctl-app")

if ( app_keychain_value == nil )
{
print("there is no such keychain value ...")
// run the script
runTerminal(NSBundle.mainBundle().resourcePath! + "/sudo_password.command")
}
}


// check if app runs from dmg
func check_for_dmg() {
// get the App's main bundle path
let resoucesPathFromApp = NSBundle.mainBundle().resourcePath!
NSLog("applicationDirectory: '%@'", resoucesPathFromApp)
//
let dmgPath: String = "/Volumes/corectl/corectl.app/Contents/Resources"
NSLog("DMG resource path: '%@'", dmgPath)
// check resourcePath and exit the App if it runs from the dmg
if resoucesPathFromApp.isEqual(dmgPath) {
// show alert message
let mText: String = "\("Corectl App cannot be started from DMG !!!")"
let infoText: String = "Please copy App to your Applications folder ..."
displayWithMessage(mText, infoText: infoText)
// exiting App
NSApplication.sharedApplication().terminate(self)
}
}


// check if corectl blobs exist
func check_for_corectl_blobs() {
let resoucesPathFromApp = NSBundle.mainBundle().resourcePath!
let bin_folder = resoucesPathFromApp + "/bin"

print(bin_folder)

let filePath1 = "/usr/local/sbin/corectl"
if (NSFileManager.defaultManager().fileExistsAtPath(filePath1))
{
print("corectl available");
}
else
{
print("corectl not available");
runScript("copy_corectl_blobs.command", arguments: bin_folder )
}

let filePath2 = "/usr/local/sbin/corectld"
if (NSFileManager.defaultManager().fileExistsAtPath(filePath2))
{
print("corectld available");
}
else
{
print("corectld not available");
runScript("copy_corectl_blobs.command", arguments: bin_folder )
}

let filePath3 = "/usr/local/sbin/corectld.runner"
if (NSFileManager.defaultManager().fileExistsAtPath(filePath3))
{
print("corectld.runner available");
}
else
{
print("corectld.runner not available");
runScript("copy_corectl_blobs.command", arguments: bin_folder )
}
}


// run script
func runScript(scriptName: String, arguments: String) {
let task: NSTask = NSTask()
let launchPath = NSBundle.mainBundle().resourcePath! + "/" + scriptName
task.launchPath = launchPath
task.arguments = [arguments]
task.launch()
task.waitUntilExit()
}


// terminal/iterm app
func runTerminal(arguments: String) {
let fileManager = NSFileManager.defaultManager()
// Check if file exists, given its path
if fileManager.fileExistsAtPath("/Applications/iTerm.app") {
// lunch iTerm App
NSWorkspace.sharedWorkspace().openFile(arguments, withApplication: "iTerm")
} else {
// lunch Terminal App
NSWorkspace.sharedWorkspace().openFile(arguments, withApplication: "Terminal")
}
}


// run an app
func runApp(appName: String, arguments: String) {
// lunch an external App
NSWorkspace.sharedWorkspace().openFile(arguments, withApplication: appName)
}


// notifications
func userNotificationCenter(center: NSUserNotificationCenter, shouldPresentNotification notification: NSUserNotification) -> Bool {
return true
}


func displayWithMessage(mText: String, infoText: String) {
let alert: NSAlert = NSAlert()
// alert.alertStyle = NSInformationalAlertStyle
// alert.icon = NSImage(named: "coreos-wordmark-vert-color")
alert.messageText = mText
alert.informativeText = infoText
alert.runModal()
}


// Adds the app to the system's list of login items.
// NOTE: This is a relatively janky way of doing this. Using a
// bundled helper app is Apple's recommended approach, but that
// has a lot of configuration overhead to get right.
func addToLoginItems() {
NSTask.launchedTaskWithLaunchPath(
"/usr/bin/osascript",
arguments: [
"-e",
"tell application \"System Events\" to make login item at end with properties {path:\"/Applications/corectl.app\", hidden:false, name:\"Corectl\"}"
]
)
}


// shell commands to run
func shell(launchPath: String, arguments: [String]) -> String
{
let task = NSTask()
task.launchPath = launchPath
task.arguments = arguments

let pipe = NSPipe()
task.standardOutput = pipe
task.launch()

let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output = String(data: data, encoding: NSUTF8StringEncoding)!
if output.characters.count > 0 {
return output.substringToIndex(output.endIndex.advancedBy(-1))

}
return output
}
2 changes: 1 addition & 1 deletion src/corectl/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>55</string>
<string>57</string>
<key>LSApplicationCategoryType</key>
<string>public.app-category.utilities</string>
<key>LSMinimumSystemVersion</key>
Expand Down
2 changes: 2 additions & 0 deletions src/fetch_latest_iso_alpha.command
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ if [[ "$CHECK_CHANNEL" == "downloading" ]]; then
echo " "
echo "You need to reload your VMs to use the lastest version !!! "
rm -f ~/.coreos/tmp/check_channel
else
echo "You have the latest ISO already ..."
fi

echo " "
Expand Down
2 changes: 2 additions & 0 deletions src/fetch_latest_iso_beta.command
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ if [[ "$CHECK_CHANNEL" == "downloading" ]]; then
echo " "
echo "You need to reload your VMs to use the lastest version !!! "
rm -f ~/.coreos/tmp/check_channel
else
echo "You have the latest ISO already ..."
fi

echo " "
Expand Down
2 changes: 2 additions & 0 deletions src/fetch_latest_iso_stable.command
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ if [[ "$CHECK_CHANNEL" == "downloading" ]]; then
echo " "
echo "You need to reload your VMs to use the lastest version !!! "
rm -f ~/.coreos/tmp/check_channel
else
echo "You have the latest ISO already ..."
fi

echo " "
Expand Down

0 comments on commit f869146

Please sign in to comment.